Custom breadcrumb depending product category

#577275
  • Resolved Anonymous
    Rank Math free

    Hello

    I’m trying to make custom Rank Math breadcrumb on product pages. I want different breadcrumbs depending of products categories.

    Here is my code for now:

    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 );	
    		
    		return $crumbs;
    	}
    	return $crumbs;
    }, 10, 2);

    This code works and return something like this :

    [HOME] - Clothing - [PRIMARY_CATEGORY]

    But I also want an option that would allow me to show breadcrumb AFTER primary category for example :

    [HOME] - Clothing - [PRIMARY_CATEGORY] - New Category

    How can I achieve this ?

    Best Regards.

Viewing 3 replies - 1 through 3 (of 3 total)
  • 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.

    Anonymous
    Rank Math free

    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 CategoryNew 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.

Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this ticket.