Hello,
Thank you for contacting Rank Math, and sorry for any inconvenience caused.
Please add this code to your website:
add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
$post_type = get_post_type(get_queried_object());
if ($post_type == 'ticket') {
$crumbs[1][0] = 'Tickets';
$crumbs[1][1] = 'http://yourCPTURL'; // Change this to match the correct URL
$crumbs[2][0] = get_the_title();
}
return $crumbs;
}, 10, 2);
If this does not work, try changing the post type to tickets in code.
Here is a guide you can follow to add the filter:
https://rankmath.com/kb/wordpress-hooks-actions-filters/
Looking forward to helping you.
Hi there, and thanks for the quick reply.
I’m aware that I can change the output of the breadcrumbs with a filter. I tested it and it works great.
add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
if( is_single() && 'ticket' == get_post_type() ) {
$crumbs[1][0] = 'Tickets';
}
return $crumbs;
}, 10, 2);
The problem is that I have a lot of CPTs, and I have to do this for all of them.
Any particular reason why the breadcrumbs are not fetching the CPT name?
I found a way to replace every CPT dynamically:
add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
if( is_single() ) {
$post_type = get_post_type( get_the_ID() );
$post_type_obj = get_post_type_object( $post_type );
$crumbs[1][0] = $post_type_obj->labels->name;
}
if( is_post_type_archive() ) {
$crumbs[1][0] = post_type_archive_title('', false);
}
return $crumbs;
}, 10, 2);
If we’re on a single post, find the CPT and get its name. Use that name in the $crumbs[1][0] array.
If we’re on a CPT archive, get the CPT archive title and use it on the same array.
It’s a lot better than going through each CPT, but I still don’t get why RankMath is not getting the CPT name instead of the slug or singular name.
Hello,
We’re glad you managed to set up the breadcrumbs to your liking.
I can see why you would prefer the CPT name as a breadcrumb however changing the default setting in Rank Math might break some setups at this point so we’ll have to stick with it.
If you need help with anything else, please let us know.
Hello,
Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.
If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.
Thank you.