Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Multiple examples of how to customize the Yoast SEO breadcrumbs
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/*
* Add shop link to the Yoast SEO breadcrumbs for a WooCommerce shop page.
* Credit: https://wordpress.stackexchange.com/users/8495/rjb
* Last Tested: Apr 20 2017 using Yoast SEO 4.6 on WordPress 4.7.3
*/
add_filter( 'wpseo_breadcrumb_links', 'wpseo_breadcrumb_add_woo_shop_link' );
function wpseo_breadcrumb_add_woo_shop_link( $links ) {
global $post;
if ( is_woocommerce() ) {
$breadcrumb[] = array(
'url' => get_permalink( woocommerce_get_page_id( 'shop' ) ),
'text' => 'Shop',
);
array_splice( $links, 1, -2, $breadcrumb );
}
return $links;
}
<?php
/********* DO NOT COPY THE PARTS ABOVE THIS LINE *********/
/*
* Remove breadcrumbs for specific items
* Credit: Yoast team
* Last Tested: Jul 21 2022 using Yoast SEO 19.3 on WordPress 6.0.1
*********
* DIFFERENT POST TYPES
* Post: Change 123456 to the post ID
* Page: Change is_single to is_page and 123456 to the page ID
* Custom Post Type: Change is_single to is_singular and 123456 to the 'post_type_slug'
Example: is_singular( 'cpt_slug' )
*********
* MULTIPLE ITEMS
* Multiple of the same type can use an array.
Example: is_single( array( 123456, 234567, 345678 ) )
* Multiple of different types can repeat the if statement
*/
add_filter( 'wpseo_breadcrumb_links', 'wpseo_breadcrumb_remove_limited' );
function wpseo_breadcrumb_remove_limited( $breadcrumbs ) {
if ( is_single ( 123456 ) ) {
return array(
'url' => '',
'text' => ''
);
}
else {
return $breadcrumbs;
}
}
@jackcarrington
Copy link

jackcarrington commented Aug 2, 2021

Hi, thanks for these.

Im trying to add the archive URL breadcrumb for posts tagged in certain categories only.

e.g. if I write a post and set the Primary category to /blog/ or /news/ - include these category URLs in the breadcrumb path.

Is there a rule to say, if the post's primary category is X, Y, Z - include the category URL in the breadcrumb path.

My problem lies with me being able to only have EVERY post-show their primary category in the breadcrumb path, or none.

Thanks in advance!

@amboutwe
Copy link
Author

amboutwe commented Aug 2, 2021

This is not the proper place to request support. Please check out our extensive help section or visit the free support forum. If you require further support, upgrading to our premium version provides you with access to our support team.

@feastdesignco
Copy link

feastdesignco commented Oct 4, 2021

Returning false on wpseo_breadcrumb_links triggers PHP warnings.

image

Is there an updated way to do this?

@amdd-tim
Copy link

amdd-tim commented Jan 6, 2022

@feastdesignco you can at least avoid PHP 8 fatal errors by returning an empty array() instead of false. It won't get rid of those null given warnings though.

@JWvanT
Copy link

JWvanT commented May 10, 2022

Hi! Thanks a lot this already helped a lot.

Kind of new to this, so hoping you can help out.

When I try to append the breadcrumb link: the old product category is still in the breadcrumb, I would like to only have the custom url there. So the custom url is in fact the top page (norway for example) with several products (travel routes to norway) under it.

But I can only get it working for 1 product page at a time. This is what I currently use:

add_filter( 'wpseo_breadcrumb_links', 'yoast_seo_breadcrumb_append_link' );

function yoast_seo_breadcrumb_append_link( $links ) {
    global $post;

    if ( is_single( 11081, 11082, 11096,  ) ) {
        $breadcrumb[] = array(
            'url' => site_url( '/ferry-to-norway-from-the-uk/' ),
            'text' => 'Ferry to Norway',
        );

        array_splice( $links, 1, -2, $breadcrumb );
    }

    return $links;
}

I would like the breadcrumb on single product page to be to be: Home » Ferry to Norway » Copenhagen-Oslo

See example on this page: https://ferrygogo.com/route/copenhagen-oslo/

@mstudioIL
Copy link

mstudioIL commented Jun 26, 2022

How can I make the last items as link?

@joseEkcit
Copy link

joseEkcit commented Jul 19, 2022

Returning false on wpseo_breadcrumb_links triggers PHP warnings.

image

Is there an updated way to do this?

Returning an array with empty (but set) 'url' and 'text' seems to solve the warning. Like: return array('url'=>'', 'text'=>'')

@amboutwe
Copy link
Author

amboutwe commented Jul 21, 2022

@joseEkcit Thank you for providing more information. I've updated the snippet now.

@vivekkotadiya
Copy link

vivekkotadiya commented Jul 29, 2022

Why this filter hook is not worked with woocommerce product category?
Here is my code:
add_filter( 'wpseo_breadcrumb_links', 'wpse_100012_override_yoast_breadcrumb_trail' );

function wpse_100012_override_yoast_breadcrumb_trail( $links ) {
global $post;

if( is_product_category() ) {
	$breadcrumb[] = array(
		'url' => get_permalink( 'my-category-custom-prefix' ),
		'text' => 'MY_CATEGORY_CUSTOM_PREFIX'
	);

	array_splice( $links, 0, -1, $breadcrumb );
}

return $links;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment