Reputation: 573
I have a php function to display breadcrumbs, however it display wrong order
for strong text example
Home / Child/ Parent / post
when it should be
Home / Parent / Child / Posts
Why?
Function
function bavota_breadcrumbs() {
if(!is_home()) {
echo '<nav class="breadcrumb">';
echo '<a href="'.home_url('/').'">'.get_bloginfo('name').'</a><span class="divider">/</span>';
if (is_category() || is_single()) {
the_category(' <span class="divider">/</span> ');
if (is_single()) {
echo ' <span class="divider">/</span> ';
the_title();
}
} elseif (is_page()) {
echo the_title();
}
echo '</nav>';
}
}
Upvotes: 1
Views: 1287
Reputation: 1976
Can you please try below code. do not use the_category display all category in which post assign to it. below function is show only it's parent category.
function bavota_breadcrumbs() {
if(!is_home()) {
echo '<nav class="breadcrumb">';
echo '<a href="'.home_url('/').'">'.get_bloginfo('name').'</a><span class="divider">/</span>';
if (is_category() || is_single()) {
//the_category(' <span class="divider">/</span> ');
global $wp_query;
$object = $wp_query->get_queried_object();
// Get parents of current category
$parent_id = $object->category_parent;
$cat_breadcrumbs = '';
while ($parent_id) {
$category = get_category($parent_id);
echo '<a href="' . get_category_link($category->cat_ID) . '">' . $category->cat_name . '</a>';
$parent_id = $category->category_parent;
echo ' <span class="divider">/</span> ';
}
echo '<span>'.$object->cat_name.'</span>';
if (is_single()) {
echo ' <span class="divider">/</span> ';
the_title();
}
} elseif (is_page()) {
echo the_title();
}
echo '</nav>';
}
}
see the_category() function here
This code is tested and work perfect.
Upvotes: 2