3

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.

1 Answer 1

4

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!

4
  • The correct filter name is wpseo_breadcrumb_links - you're missing the 'o' in wpseo in your function name/filter in the code block.
    – Michelle
    Commented Jun 6, 2019 at 14:47
  • 1
    Updated above - good catch!
    – WebElaine
    Commented Jun 6, 2019 at 16:19
  • 1
    If you want to always return the breadcrumbs the line return $links; should be placed after the if block not inside.
    – Luckyfella
    Commented Nov 26, 2019 at 10:12
  • Also updated :)
    – WebElaine
    Commented Dec 2, 2019 at 18:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.