I’m trying to change the link in the default Yoast SEO Breadcrumbs.
The current Breadcrumb is
Home / Projects / My Pretty Project
And I want to change the “Projects”, and it’s link, to “Case Studies:
Home / Case Studies / My Pretty Project
This is what I’ve been trying to get working, using the wpseo_breadcrumb_output hook:
add_filter( 'wpseo_breadcrumb_output', 'custom_wpseo_breadcrumb_output' );
function custom_wpseo_breadcrumb_output( $output ){
if( is_single () ){
$from = '/projects/">Projects</a>';
$to = '/case-studies/">Case Studies</a>';
$output = str_replace( $from, $to, $output );
}
return $output;
}
Any help or pointers to get this working?
Thank you!
Edit.. still haven’t got it.
That’s an unusual setup – Yoast usually recognizes the right permalinks. You may want to consider changing the way you set up your custom post type, so that it has the desired names and URLs to begin with. You’re likely to have two different URLs that point to the same content, which is undesirable for SEO and sometimes confusing to end users.
However, to answer your original question, you can use another filter called wpseo_breadcrumb_links
:
// Hook to the filter
add_filter('wpseo_breadcrumb_links', 'wpse_332125_breadcrumbs');
// $links are the current breadcrumbs
function wpse_332125_breadcrumbs($links) {
// Use is_singular($post_type) to identify a single CPT
// This assumes your CPT is called "project" - change it as needed
if(is_singular('project')) {
// The first item in $links ($links[0]) is Home, so skip it
// The second item in $links is Projects - we want to change that
$links[1] = array('text' => 'Case Studies', 'url' => '/case-studies/', 'allow_html' => 1);
}
// Even if we didn't change anything, always return the breadcrumbs
return $links;
}
You’ll want to make sure to test everything thoroughly, to make sure both the original links and the breadcrumbs all work as expected. Hope this helps you and others, as it took me a long time to stumble on this almost-undocumented filter!