Remove all categories from breadcrumbs

#569009
  • Resolved Dre
    Rank Math free

    Hi,
    I know I can remove all product categories from Rank Maths Breadcrumbs on the single product pages in the settings, but I do need to keep a primary product category because it is needed for another use. So my issue/question is: how can remove all parent categgories from breadcrumbs, so tht the structure is

    Home / Product

    and STILL KEEP a primary product category for every product?

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

    Dre
    Rank Math free

    Thank you, the second option was what I was looking for. Perfect!

    Hello,

    We’re super happy that we could address your concern.

    Please feel free to reach out to us again in case you need any other assistance.

    We are here to help.

    Thank you.

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

The ticket ‘Remove all categories from breadcrumbs’ is closed to new replies.