The Yoast SEO plugin is a popular choice for optimizing content for search engines, and it includes a feature for generating breadcrumbs. Breadcrumbs are a navigational aid that helps users understand the site structure and easily navigate back to previous pages.
This hook allows users to add a custom “Blog” link in the Yoast SEO breadcrumb when viewing a single entry, providing a more personalized and convenient navigation experience.
Code:
// Add a filter to modify Yoast SEO breadcrumb links
add_filter( 'wpseo_breadcrumb_links', 'yoast_seo_breadcrumb_blog_append_link' );
// Callback function to modify breadcrumbs
function yoast_seo_breadcrumb_blog_append_link( $links ) {
// Check if the current page is a single post
if ( is_singular( 'post' ) ) {
// Create a new breadcrumb item for the blog
$breadcrumb[] = array(
'url' => site_url( '/blog/' ), // You can specify any link; in this example, it leads to a static "blog" page
'text' => 'Blog', // The display text for the breadcrumb item
);
// Insert the new breadcrumb item after the first item (usually "Home") and remove all items starting from the second to the second-to-last
array_splice( $links, 1, -2, $breadcrumb );
}
// Return the modified breadcrumbs
return $links;
}
Leave a Reply