skip to Main Content

I want to remove the link to the products archive in the breadcrumbs on all pages.

To do that, I found a solution in the Yoast docs. It works fine except on the product pages.
Here’s my current code:

/* Remove "Products" from Yoast SEO breadcrumbs in WooCommerce */
add_filter( 'wpseo_breadcrumb_links', function( $links ) {

    // Check if we're on a WooCommerce page
    // Checks if key 'ptarchive' is set
    // Checks if 'product' is the value of the key 'ptarchive', in position 1 in the links array
    if ( is_woocommerce() && isset( $links[1]['ptarchive'] ) && 'product' === $links[1]['ptarchive'] ) {

        // True, remove 'Products' archive from breadcrumb links
        unset( $links[1] );

    // Added by me to remove it on product single pages - doesn't work!
    } elseif ( is_product() && isset( $links[1]['ptarchive'] ) && 'product' === $links[1]['ptarchive'] ) {

        // True, remove 'Products' archive from breadcrumb links
        unset( $links[1] );


    }

    // Rebase array keys
    $links = array_values( $links );

    // Return modified array
    return $links;

});

It removes the link on archive pages and all other WooCommerce pages as you would expect by the conditional tag is_woocommerce().

But for some reason it won’t work on the single product page.
As you can see, I added an extra check if I’m on a product page with is_product(). Unfortunately that doesn’t work. And is_singular('proudct') don’t worl either.

Maybe there is something wring wihth the $links array?
But I’m not sure how to check that.

2

Answers


  1. Chosen as BEST ANSWER

    I changed the if condition to this:

    if ( is_product() ) {
        // True, remove 'Products' archive from breadcrumb links
        unset( $links[1] );
    }
    

    Now it works. The other pages worked before without the function.


  2. You can also remove ‘shop’ or ‘products’ links on shop pages by adding ‘ || is_archive() ‘ to if conditions like so:

    if ( is_product()  || is_archive() ) {
    // True, remove 'Products' archive from breadcrumb links
        unset( $links[1] );
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search