-
Hi,
I’ve got some posts that have more than one category. Problem is Rankmath’s breadcrumbs seems to pick a random category instead of the parent category by default.
For instance, if you look at this post (https://wordpress-614107-2226950.cloudwaysapps.com/most-forgiving-irons/) on my staging site, the breadcrumbs are showing as one of the child categories. The post is set with a different parent category in this case “Top Iron Set Pick” which is a different category completely.
Some post show the right pathway, however, it seems to be inconsistent.
-
Hello,
Thanks for contacting us, and sorry for any inconvenience that might have been caused due to that.
I can see you currently have the structure of your breadcrumb: Home > Golf Gear > Iron Sets > 7 Most Forgiving Irons: Know The Why Before You Buy
Here
Golf Gear
seems to be the parent category andIron Sets
is the child.Looks like it’s set up correctly. Can you please share a bit more detail on What structure you’re looking for?
Looking forward to helping you.
So that link’s parent category is in fact another category called “Beginner Golf Buying Guide”. What is shown now on that link is a different category that is a secondary category.
For instance, look at the 1st article and the 2nd article, they should have the same breadcrumbs. Both share two categories, and both have the parent category as shown in the 2nd link. But the first link doesn’t show otherwise.
https://wordpress-614107-2226950.cloudwaysapps.com/best-small-head-drivers/
https://wordpress-614107-2226950.cloudwaysapps.com/taylormade-stealth-2-driver-review/
Hello,
Please try adding the following code snippet on your website to make sure that all categories are displayed hierarchically on the blog posts:
add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) { if ( is_single() ) { $b_items = count( $crumbs ); $categories = get_the_category(); if ( $categories ) { $i = 1; foreach( $categories as $category ) { $ccrumbs[ $i ][0] = $category->name; $ccrumbs[ $i ][1] = get_category_link( $category->term_id ); $ccrumbs[ $i ]['hide_in_schema'] = false; $parent_id = $category->parent; while ( $parent_id ) { $i++; $parent = get_term( $parent_id, 'category' ); $ccrumbs[ $i ][0] = $parent->name; $ccrumbs[ $i ][1] = get_category_link( $parent->term_id ); $ccrumbs[ $i ]['hide_in_schema'] = false; $parent_id = $parent->parent; if ( $parent_id == 0 ) { break; } $i++; } } $ccrumbs = array_reverse($ccrumbs); array_splice($crumbs, 1, ($length - 1), $ccrumbs); array_unique($crumbs, SORT_REGULAR); } } return $crumbs; }, 10, 2);
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.
Thank you.
https://wordpress-614107-2226950.cloudwaysapps.com/best-small-head-drivers/
Thanks for the code! it uniformed everything, but in fact merged two categories together while ignoring top layer categories.
The result of the breadcrumbs for the page should be Home > Tee Off Tips > Driver Buying Guide > Driver Top Picks > article
Is there a way to modify the code to do this?
Hello,
Are you looking for the breadcrumbs structure only for the specific URL you shared?
Home > Tee Off Tips > Driver Buying Guide > Driver Top Picks > article
Looking forward to hearing back from you.
Thank you.
Sorry for the late reply,
For those pages, yes that breadcrumb pathway.
But I need the pathway to reflect the primary category set for that specific page.
For instance, let’s take the small head driver article, which has been set into two categories for my site:
Tee Off Tips > Driver Buying Guide > Driver Top Picks > article
&
Golf gear > Drivers> Article
The first pathway is the primary category set for this article (Driver Top Picks) and the Drivers category as part of Golf gear is a different category on its own that this article is also assigned to.
This set of rules is also the applied to the ‘TaylorMade stealth 2 driver review article’.
However, both show different breadcrumb pathways. I would assume the default pathway would be set to the primary category set for this article, in this case (Tee Off Tips > Driver Buying Guide > Driver Top Picks > article) but if you look at ‘small head driver’ it’s not!
In other words, I want the breadcrumbs to register the pathway of the primary category, not a secondary (non priority) one.
Hello,
If you have selected the Driver Top Picks as the primary category, then that path should be displayed on the breadcrumbs.
However, the breadcrumbs are showing different paths for that URL:
https://wordpress-614107-2226950.cloudwaysapps.com/best-small-head-drivers/In this case, we might need to take a closer look at the settings. Please edit the first post on this ticket and include your WordPress & FTP logins in the designated Sensitive Data section.
Please do take a complete backup of your website before sharing the information with us.
It is completely secure and only our support staff has access to that section. If you want, you can use the below plugin to generate a temporary login URL to your website and share that with us instead:
https://wordpress.org/plugins/temporary-login-without-password/
You can use the above plugin in conjunction with the WP Security Audit Log to monitor what changes our staff might make on your website (if any):
https://wordpress.org/plugins/wp-security-audit-log/
We really look forward to helping you.
Thank you.
Hello,
I have updated the sensitive data as requested. Can you please check further?
Thank you.
Hello,
I’ve updated the code with the following one:
add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) { if ( is_single() ) { $b_items = count( $crumbs ); $primary_term_id = get_post_meta( get_queried_object_id(), 'rank_math_primary_category', true ); $primary_term = get_term_by( 'id', $primary_term_id, 'category' ); if ( $primary_term ) { $i = 1; $ccrumbs[ $i ][0] = $primary_term->name; $ccrumbs[ $i ][1] = get_category_link( $primary_term->term_id ); $ccrumbs[ $i ]['hide_in_schema'] = false; $parent_id = $primary_term->parent; while ( $parent_id ) { $i++; $parent = get_term( $parent_id, 'category' ); $ccrumbs[ $i ][0] = $parent->name; $ccrumbs[ $i ][1] = get_category_link( $parent->term_id ); $ccrumbs[ $i ]['hide_in_schema'] = false; $parent_id = $parent->parent; if ( $parent_id == 0 ) { break; } $i++; } $ccrumbs = array_reverse($ccrumbs); array_splice($crumbs, 1, ($length - 1), $ccrumbs); array_unique($crumbs, SORT_REGULAR); } } return $crumbs; }, 10, 2);
From now on, breadcrumbs will only show the pathway of the primary category, not a secondary (non-priority) one. Please check on your end if everything is working as expected or not.
Looking forward to helping you.
Thank you.
Hi,
Upon opening on mobile even through incognito I see these codes showing on top. And bottom of the sidebar.
It doesn’t look right.
I can’t send an image over unless it has a URL.
But trying opening one of the links above through your mobile and you see what I’m saying.
Hello,
Looks like you were seeing the cached version of those URLs. I’ve cleared the cache and you shouldn’t encounter any issues now. Can you please check and let us know?
Looking forward to helping you.
Thank you.
Hi,
All fixed thanks guys!
Just curious though, what went wrong? As I heard by Reinelle that the breadcrumbs should be by default show the primary category path.
Why was a code needed?
Hello,
Glad to know that it’s working.
It looks like there were some structural issues with the specific affected URL that was not picking up the correct one.
However, there’s nothing to worry about now as the code will force to follow the correct path.
Hope that helps.
Thank you.
Noted,
Thank you Sakib 🙂
Saved me alot of money as I was about to buy a new breadcrumb plugin!
Thank you!
Hello,
You’re most welcome, and 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.
The ticket ‘Breadcrumbs not showing based on Parent Category’ is closed to new replies.