The best WordPress plugin for generating breadcrumbs I could find is Breadcrumb NavXT. It’s easy to install and has a lot of customization options to suit your blogging needs. However, there are some problems if you use multiple categories.
If my post contains more than one parent or child category, wich one does NavXT pick to use in the breadcrumb? In what order are the categories selected?
The answer is that NavXT searches in the post settings for the first child category it finds, and if it doesn’t find a child category, it will display the first parent category.
What is the first child or parent category? you ask.
The first child or parent category is random, in fact all the categories within the post are returned randomly.
TL;DR How do I solve this randomness issue?
- Replace the contents of your plugins/breadcrumb-navxt/breadcrumb_navxt_class.php file with mine or just the lines 351 to 359 with
//Read the breadcrumb-category custom field $breadcrumb_category = get_post_meta($post->ID, 'breadcrumb-category',true); //Fallback functionality in case the breadcrumb-category custom field is not set $first_term_found = 0; foreach($bcn_object as $key=>$object) { //We want the breadcrumb category setting for this post, if it's set if(($object->slug == $breadcrumb_category) and ($breadcrumb_category != '')) { $bcn_use_term = $key; //We found our breadcrumb category, can exit loop now break; } //No breadcrumb category has been found yet. We want the first term hiearchy, if we haven't found it already elseif(($object->parent > 0) and (!$first_term_found)) { $bcn_use_term = $key; //We found our first term hiearchy, can exit loop now if no breadcrumb category is set if ($breadcrumb_category == '') break; else $first_term_found = 1; //won't exit loop until we end the search for breadcrumb-category set } - Edit your post and create a custom field with the name breadcrumb-category and the value containing your desired category slug. Don’t forget to click Add Custom Field.
- Refresh your article page.
- ???
- Profit!
Use the comment form below if you have any problems with it. Good luck!
PS: First I thought that the solution might be a plugin that sorts categories, such as My Category Order, but it only works within widgets. Don’t try it, been there done that.