Altering breadcrumbs “Follow up question”

#938271
  • Resolved Aaron Dulpina
    Rank Math free

    Hi, I found this support ticket and the provided solution works. Link here

    My only issue is that my breadcrumbs has this excess “separator” which should also not be rendered. Any ideas how to remove that from the hook? Thank you

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

    Thank you for your query and we are so sorry about the trouble this must have caused.

    To remove the breadcrumbs separator, you need to unset the whole breadcrumbs item from the array or object $crumbs.

    Can you please share the custom breadcrumbs code you created so we can take a closer look?

    Looking forward to helping you.

    Hi Jeremy,

    Thanks for the reply.

    I actually am using the unset method instead of the crumbs[$i]=” method.

    //function to remove customPostType in the breadcrumbs
    add_filter( ‘rank_math/frontend/breadcrumb/items’, function( $crumbs, $class ) {
    if(get_post_type() == ‘customPostType’ && is_single()) { //Change this to match the post type name
    unset($crumbs[1]); // unset ‘customPostType’ breadcrumb so it will not appear
    }
    return $crumbs;
    }, 10, 2);

    So this code does remove the intended breadcrumb item but leaves an output like this

    Home >> sampleCategory >>

    Hello,

    Please add the following code and see if this would work on your end:

    add_filter( 'rank_math/frontend/breadcrumb/html', function( $html, $crumbs, $class ) {
    	$separator = '<span class="separator"> >> </span>';
    	
    	if (preg_match('/' . preg_quote($separator, '/') . '\s*<\/p>\s*<\/nav>$/', $html)) {
    		$html = preg_replace('/' . preg_quote($separator, '/') . '\s*<\/p>\s*<\/nav>$/', '</p></nav>', $html);
    	}
    
    	return $html;
    }, 10, 3);

    You need to make sure that the $separator variable matches the actual trailing separator you want to remove.

    Hope that helps.

    Thank you.

    Aaron Dulpina
    Rank Math free

    Hi Jeremy,

    I tried your plugin but unfortunately it did not work on my end.

    I can confirm that the hook does work if I make it return an empty string without your “if” condition.Also testing the preg replace outside the if condition does not change the output.

    Keep in my that I am using the default RankMath separator » and I updated the $separator with that value but still does not work.

    Thank you for your assistance.

    Aaron Dulpina
    Rank Math free

    Hello again,

    I have an update on the issue, so it seems that if I decide to not hide the post title, the code will not work properly.

    Let’s say the output is

    Home » CategoryToBeRemoved » SampleCategory » Post Title

    When I do unset using the code:

    //function to remove customPostType in the breadcrumbs
    add_filter( ‘rank_math/frontend/breadcrumb/items’, function( $crumbs, $class ) {
    if(get_post_type() == ‘customPostType’ && is_single()) { //Change this to match the post type name
    unset($crumbs[1]); // unset ‘customPostType’ breadcrumb so it will not appear
    }
    return $crumbs;
    }, 10, 2);

    It returns it like this for some reason.

    Home » SampleCategoryPost Title »

    with the Post Title having the link and not the Sample Category

    Aaron Dulpina
    Rank Math free

    Hello,

    I will be closing this ticket now.

    I managed to resolve my issue with the help of the hook you provided me.

    Thank you

    Hello,

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

    If you don’t mind us 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 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 7 replies - 1 through 7 (of 7 total)

The ticket ‘Altering breadcrumbs “Follow up question”’ is closed to new replies.