Hello,
Thank you for contacting Rank Math and bringing your concern to our attention.
You can add another condition in your filter to include the “New Category” and replace the $value
variables and the positioning in ‘$crumbs’ like in this filter (applies with Clothing crumbs like in your example):
add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
if ( is_product() ) {
if ( has_term (array('t-shirts','sweaters','jackets'), 'product_cat') ) {
$value[] = array(
'Clothing',
'/category/clothing',
'hide_in_schema' => false
);
}
if ( has_term (array('running-shoes','work-shoes','boots'), 'product_cat') ) {
$value[] = array(
'Shoes',
'/category/shoes',
'hide_in_schema' => false
);
}
array_splice( $crumbs, 1, 0, $value );
if ( is_product() ) {
if ( has_term (array('t-shirts','sweaters','jackets'), 'product_cat') ) {
$value1[] = array(
'New Category',
'/category/newcategoryslug',
'hide_in_schema' => false
);
}
array_splice( $crumbs, 3, 0, $value1 );
}
return $crumbs;
}
}, 10, 2);
You can update the word new category and the slug in the filter with the exact name and URL.
Hope that helps.
Thank you.
Perfect, thank you for your answer, works great !
Just one last question, how to put multiple values in one array to lighten the code when I want to show multiple categories in a row ?
I tried like this but does not work :
$value1[] = array(
'New Category',
'/category/newcategoryslug',
'New Category 2',
'/category/newcategory2slug',
'hide_in_schema' => false
);
I want to return this with one array :
[HOME] – Clothing – [PRIMARY_CATEGORY] – New Category – New Category 2
Thanks a lot.
Hello,
You can include it inside the first if statement, and create the variable there ($value1, $value2, and so on..), then return that value on the array_splice line below.
Here’s the updated filter you can try as well to achieve the example you have shared:
add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
if ( is_product() ) {
if ( has_term (array('t-shirts','sweaters','jackets'), 'product_cat') ) {
$value[] = array(
'Clothing',
'/category/clothing',
'hide_in_schema' => false
);
$value1[] = array(
'New Category',
'/category/newcategoryslug',
'hide_in_schema' => false
);
$value2[] = array(
'New Category 2',
'/category/newcategory2slug',
'hide_in_schema' => false
);
}
if ( has_term (array('running-shoes','work-shoes','boots'), 'product_cat') ) {
$value[] = array(
'Shoes',
'/category/shoes',
'hide_in_schema' => false
);
}
array_splice( $crumbs, 1, 0, $value );
array_splice( $crumbs, 3, 0, $value1 );
array_splice( $crumbs, 4, 0, $value2 );
}
return $crumbs;
}, 10, 2);
Hope that helps.
Thank you.