Altering breadcrumbs – skipping selected categories

#824531
  • Resolved Kamil
    Rank Math free

    Hello. My shop is quite small now but I don’t want to do category changes in future. Let’s say current breadcrumbs are: Shop => Men’s clothes => T-Shirts => Tops. But tops have only 1 product, I don’t want people clicking it in breadcrumbs. Can I disable specific categories in breadcrumbs?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Hello,

    Thank you for contacting Rank Math and bringing your concern to our attention.

    If you want to customize the breadcrumbs for that category to be unclickable but still show on the breadcrumbs, you can use this filter on your site:

    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
        if(get_post_type() == 'post' && is_single()) { //Change this to match the post type name
    		$crumbs[3][1] = ''; 
        }
        return $crumbs;
    }, 10, 2);

    The code above assumes that “Shop” is the first crumb. If you have “Home” as the first, you can update this line in the code $crumbs[4][1]

    However, if you want to completely remove it, you can change the third line to this unset ($crumbs[3]);

    If you’re not sure how to add this code, you can follow this guide:
    https://rankmath.com/kb/wordpress-hooks-actions-filters/

    Hope that helps.

    Kamil
    Rank Math free

    Thanks, but structure is different for different categories, sometimes it’s 1>2>3>4 and someitmes 1>2>3. Can’t i filter specifically by category id/slug?

    Hello,

    The $crumbs variable is not a term object so you can only check the name or the URL.

    You may try this modified code to only modify the preferred breadcrumbs:

    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    	if(is_product()) {
    		for($i = 0; $i < count($crumbs); $i++){
    			if($crumbs[$i][0] == 'T-Shirts'){
    				$crumbs[$i][1] = '';
    			}
    		}
    	}
    	return $crumbs;
    }, 10, 2);

    If you need more than one category to exclude, you can add more condition statements like so:

    if($crumbs[$i][0] == 'T-Shirts' || $crumbs[$i][0] == 'ANOTHER CATEGORY'){
    	$crumbs[$i][1] = '';
    }

    Hope that helps.

    Kamil
    Rank Math free

    Awesome, thanks

    Hello,

    We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

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

The ticket ‘Altering breadcrumbs – skipping selected categories’ is closed to new replies.