Skip to content

Commit

Permalink
Merge pull request #9 from ItinerisLtd/fix-yoast-breadcrumbs
Browse files Browse the repository at this point in the history
fix Yoast breadcrumbs
  • Loading branch information
danlapteacru authored Nov 15, 2022
2 parents 0413ab5 + 4955eba commit 7e2d97c
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Types/CustomPostType.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct()
{
add_filter('display_post_states', [$this, 'addPageStates'], 10, 2);
add_filter('register_post_type_args', [$this, 'registerPostTypeArgs'], 10, 2);
add_filter('wpseo_breadcrumb_links', [$this, 'breadcrumbLinks']);
add_action('template_redirect', [$this, 'customTemplate']);
add_action('admin_init', [$this, 'addCustomPostTypePageSelectorOptions']);
add_action('customize_register', [$this, 'customizerRegister']);
Expand Down Expand Up @@ -281,4 +282,43 @@ public function customTemplate(): void
]);
exit;
}

public function breadcrumbLinks(array $links): array
{
$postTypes = array_keys($this->getPostTypes());
if (empty($postTypes)) {
return $links;
}

if (! is_singular($postTypes) && ! is_post_type_archive($postTypes)) {
return $links;
}

$post_type = get_post_type();
$archive_page_id = (int) get_option("page_for_{$post_type}", 0);
if (0 === $archive_page_id) {
return $links;
}

$pages_id = [$archive_page_id];
$ancestors = get_post_ancestors($archive_page_id);
$pages_id = array_reverse(array_merge($pages_id, $ancestors));

$breadcrumbs = [];

foreach ($pages_id as $crumb) {
$breadcrumbs[] = [
'url' => get_permalink($crumb),
'text' => get_the_title($crumb),
];
}

if (is_single()) {
array_splice($links, 1, -1, $breadcrumbs);
} else {
array_splice($links, 1, 1, $breadcrumbs);
}

return $links;
}
}

0 comments on commit 7e2d97c

Please sign in to comment.