Hello,
Thank you for contacting Rank Math and bringing your concern to our attention.
Do you want to keep the primary product category in the breadcrumbs? If so, please add the following filter on your site:
add_filter('rank_math/frontend/breadcrumb/items', function ($crumbs, $class) {
if ( is_product() ) {
global $product;
$categories = get_the_terms( $product->ID, 'product_cat' );
foreach($categories as $category){
$category = $category->name;
$product_primary_term = get_term(get_post_meta(get_queried_object_id(), 'rank_math_primary_product_cat', true));
$exclude = $product_primary_term->name;
if ($exclude != $category){
$key = array_search($category, array_column($crumbs, '0'), true);
}
if (isset($key) && ($key != false ) ) {
unset($crumbs[$key]);
$crumbs = array_values($crumbs);
}
}
}
return $crumbs;
}, 10, 2);
Expected Output:
Home / YOUR Product’s Primary Category / Product name (Keeps the primary category of your product in breadcrumbs).
To remove all categories including the primary product category, please apply the following filter:
add_filter('rank_math/frontend/breadcrumb/items', function ($crumbs, $class) {
if ( is_product() ) {
global $product;
$categories = get_the_terms( $product->ID, 'product_cat' );
foreach($categories as $category){
$category = $category->name;
$key = array_search($category, array_column($crumbs, '0'), true);
if (isset($key) && ($key != false ) ) {
unset($crumbs[$key]);
$crumbs = array_values($crumbs);
}
}
}
return $crumbs;
}, 10, 2);
Expected Output:
Home / Product Name (Removes all categories from breadcrumbs in your products).
Note:
The codes above don’t have any impact on your actual primary product category. The code only applies to your product breadcrumbs.
Here’s how you can add filters/hooks to your WordPress site:
https://rankmath.com/kb/wordpress-hooks-actions-filters/
Let us know how it goes. Looking forward to helping you.