Skip to content
Advertisement

WooCommerce: Remove “Products” archive from Yoast breadcrumbs

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.

Advertisement

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement