Change category link in breadcrumbs for a specific category

#480520
  • Resolved Jon Wright
    Rank Math free

    Hi – I have one specific category where I would like the breadcrumb category link to go to a specific page rather than the category.

    I spotted this ticket https://support.rankmath.com/ticket/how-to-change-link-on-breadcrumbs/ and I modified the code accordingly but it doesn’t work (it works without the IF statement that I added, so I assume I’ve got that part wrong). Could you tell me the correct code please.

    
    
    /**
     * Allow changing or removing the Breadcrumb items
     *
     * @param array       $crumbs The crumbs array.
     * @param Breadcrumbs $this   Current breadcrumb object.
     */
    if ( in_category('alternatives') ) {
    	add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
        	$crumbs[1][1] = '/alternatives'; // Add the desired URL
        	return $crumbs;
    	}, 10, 2);
    }
    
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    Thanks for contacting us, and sorry for any inconvenience that might have been caused due to that.

    The filter you’ve tried to add will work too if you add the if condition inside the filter.

    However, here’s a customized filter below to change that category URL on breadcrumbs:

    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    	$categories = array_map( function ( $category ) {
    		return $category->slug;
    	}, get_the_category() );
    	
    	if ( in_array( 'alternatives', $categories ) ) { //replace 'alternatives' with your category slug
    	   	$crumbs[1][1] = '/alternatives'; // Add the desired URL
    	}
       	return $crumbs;
    }, 10, 2);

    Let us know how it goes. Looking forward to helping you.

    Thank you.

    That works great. Thank you.

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

You must be logged in to reply to this ticket.