Ask your WordPress questions! Pay money and get answers fast! (more info)

Category parents in breadcrumb navigation WordPress

Hi there! I'm having a breadcrumb navigation on single pages, which works like this:

<em>Homepage - > The post's category -> The post.</em>

I would like the navigation to also show the category parent. Like this:

<em>Homepage -> <strong>Category Parent </strong>-> Category Child -> The post.</em>

Below is my code. How can I modify it to also show the category parent?

function the_breadcrumb() {
echo '<ul id="crumbs">';
if (!is_home()) {
echo '<li><a href="';
echo get_option('home');
echo '">';
echo 'Homepage';
echo "</a><span>/</span></li>";
if (is_category() || is_single()) {
echo '<li>';
the_category('<span>/</span></li><li>');
if (is_single()) {
echo "<span>/</span></li><li>";
the_title();
echo '</li>';
}
} elseif (is_page()) {
echo '<li>';
echo the_title();
echo '</li>';
}
}
elseif (is_tag()) {single_tag_title();}
elseif (is_day()) {echo"<li>Archive for "; the_time('F jS, Y'); echo'</li>';}
elseif (is_month()) {echo"<li>Archive for "; the_time('F, Y'); echo'</li>';}
elseif (is_year()) {echo"<li>Archive for "; the_time('Y'); echo'</li>';}
elseif (is_author()) {echo"<li>Author Archive"; echo'</li>';}
elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<li>Blog Archives"; echo'</li>';}
elseif (is_search()) {echo"<li>Search Results"; echo'</li>';}
echo '<div class="clear"></div></ul>';
}


Thanks!

Answers (7)

2013-08-17

Abdelhadi Touil answers:

Hi.
I'm using this function and it works perfectly:

[[LINK href="http://dimox.net/wordpress-breadcrumbs-without-a-plugin/"]]http://dimox.net/wordpress-breadcrumbs-without-a-plugin/[[/LINK]]

Of course you should use it instead of your actual function, then use it in your theme.
Good luck!

2013-08-17

Sabby Sam answers:

You can use this plugin
http://wordpress.org/plugins/full-breadcrumb/

and

http://wordpress.org/plugins/breadcrumbs/


or

This discussion will help you
http://wp-snippets.com/breadcrumbs-without-plugin/

elseif (is_page()) {
if($post->post_parent){
echo '<li>'.get_the_title($post->post_parent).'</li>';
} else {
echo '<li>';
echo the_title();
echo '</li>';
}
}

2013-08-17

Ellah Alzona answers:

EXAMPLE OUTPUT: Example: MainCategoryName > Child > Child > Child > Post title

Add this to your function.php

function bloglow_get_breadcrumb_navigation() {

$delimiter = '&raquo;';

$home = get_bloginfo('name');

$before = '<span>';

$after = '</span>';

echo '
<div id="breadcrumb"><!-- Bloglow breadcrumb navigation without a plugin v1.0 - http://bloglow.com/plugins/display-wordpress-breadcrumb-navigation-without-a-plugin/ -->';

global $post;

$homeLink = get_bloginfo('url');

echo '<a href="' . $homeLink . '">' . $home . '</a> ' . $delimiter . ' ';

if ( is_category() ) {

global $wp_query;

$cat_obj = $wp_query->get_queried_object();

$thisCat = $cat_obj->term_id;

$thisCat = get_category($thisCat);

$parentCat = get_category($thisCat->parent);

if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));

echo $before . 'Archive by category "' . single_cat_title('', false) . '"' . $after;

} elseif ( is_day() ) {

echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';

echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';

echo $before . 'Archive by date "' . get_the_time('d') . '"' . $after;

} elseif ( is_month() ) {

echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';

echo $before . 'Archive by month "' . get_the_time('F') . '"' . $after;

} elseif ( is_year() ) {

echo $before . 'Archive by year "' . get_the_time('Y') . '"' . $after;

} elseif ( is_single() && !is_attachment() ) {

if ( get_post_type() != 'post' ) {

$post_type = get_post_type_object(get_post_type());

$slug = $post_type->rewrite;

echo '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>' . $delimiter . ' ';

echo $before . get_the_title() . $after;

} else {

$cat = get_the_category(); $cat = $cat[0];

echo ' ' . get_category_parents($cat, TRUE, ' ' . $delimiter . ' ') . ' ';

echo $before . 'You&apos;re currently reading "' . get_the_title() . '"' . $after;

}

} elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {

$post_type = get_post_type_object(get_post_type());

echo $before . $post_type->labels->singular_name . $after;

} elseif ( is_attachment() ) {

$parent_id = $post->post_parent;

$breadcrumbs = array();

while ($parent_id) {

$page = get_page($parent_id);

$breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';

$parent_id = $page->post_parent;

}

$breadcrumbs = array_reverse($breadcrumbs);

foreach ($breadcrumbs as $crumb) echo ' ' . $crumb . ' ' . $delimiter . ' ';

echo $before . 'You&apos;re currently viewing "' . get_the_title() . '"' . $after;

} elseif ( is_page() && !$post->post_parent ) {

echo $before . 'You&apos;re currently reading "' . get_the_title() . '"' . $after;

} elseif ( is_page() && $post->post_parent ) {

$parent_id = $post->post_parent;

$breadcrumbs = array();

while ($parent_id) {

$page = get_page($parent_id);

$breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';

$parent_id = $page->post_parent;

}

$breadcrumbs = array_reverse($breadcrumbs);

foreach ($breadcrumbs as $crumb) echo ' ' . $crumb . ' ' . $delimiter . ' ';

echo $before . 'You&apos;re currently reading "' . get_the_title() . '"' . $after;

} elseif ( is_search() ) {

echo $before . 'Search results for "' . get_search_query() . '"' . $after;

} elseif ( is_tag() ) {

echo $before . 'Archive by tag "' . single_tag_title('', false) . '"' . $after;

} elseif ( is_author() ) {

global $author;

$userdata = get_userdata($author);

echo $before . 'Articles posted by "' . $userdata->display_name . '"' . $after;

} elseif ( is_404() ) {

echo $before . 'You got it "' . 'Error 404 not Found' . '"&nbsp;' . $after;

}

if ( get_query_var('paged') ) {

if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';

echo ('Page') . ' ' . get_query_var('paged');

if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';

}

echo '</div>
<!-- / Bloglow breadcrumb navigation without a plugin -->';


and in your single.php

<?php bloglow_get_breadcrumb_navigation(); ?>



EXAMPLE OUTPUT: Example: MainCategoryName > Child > Child > Child > Post title

Hope this helps.

2013-08-17

tong chen answers:

function the_breadcrumb() {
echo '<ul id="crumbs">';
if (!is_home()) {
echo '<li><a href="';
echo get_option('home');
echo '">';
echo 'Homepage';
echo "</a><span>/</span></li>";
if (is_category()) {
echo '<li>';
the_category('<span>/</span></li><li>');
echo '</li>'; //I think you should add this
}else(is_single()){
$category = get_the_category();
echo '<li>';
if($category[0]){
echo get_category_parents($category[0], true,'<span>/</span></li><li>');
}
echo '</li><span>/</span><li>'. get_the_title().'</li>';
} elseif (is_page()) {

echo '<li>';

echo the_title();

echo '</li>';
}
}
elseif (is_tag()) {single_tag_title();}
elseif (is_day()) {echo"<li>Archive for "; the_time('F jS, Y'); echo'</li>';}
elseif (is_month()) {echo"<li>Archive for "; the_time('F, Y'); echo'</li>';}
elseif (is_year()) {echo"<li>Archive for "; the_time('Y'); echo'</li>';}
elseif (is_author()) {echo"<li>Author Archive"; echo'</li>';}
elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<li>Blog Archives"; echo'</li>';}
elseif (is_search()) {echo"<li>Search Results"; echo'</li>';}
echo '<div class="clear"></div></ul>';
}

2013-08-17

zebra webdesigns answers:

Hello Jens

please find the code which gives the exact output as you want.

place the below line the header file

<?php the_breadcrumb(); ?>

Baste the below line in the theme functions file



function the_breadcrumb() {

echo '<span id="crumbs">';

if (!is_home()) {

echo '<span><a href="';

echo get_option('home');

echo '">';

echo 'Homepage';

echo "</a><span> -> </span>";

if (is_category()) {

echo '<span>';

the_category('<span>/</span></span><span>');

echo '</span>'; //I think you should add this

}else if(is_single()){

$cat = get_the_category();

echo '';

if($cat[0]){

echo get_category_parents($cat[0], true,'<span> -> </span>');
}
echo '<span>'. get_the_title().'</span>';

} elseif (is_page()) {
echo '<span>';
echo the_title();
echo '</span>';
}
}

elseif (is_tag()) {single_tag_title();}

elseif (is_day()) {echo"<span>Archive for "; the_time('F jS, Y'); echo'</span>';}

elseif (is_month()) {echo"<span>Archive for "; the_time('F, Y'); echo'</span>';}

elseif (is_year()) {echo"<span>Archive for "; the_time('Y'); echo'</span>';}

elseif (is_author()) {echo"<span>Author Archive"; echo'</span>';}

elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<span>Blog Archives"; echo'</span>';}

elseif (is_search()) {echo"<span>Search Results"; echo'</span>';}

echo '<div class="clear"></div></span>';

}



2013-08-17

Liam Bailey answers:

I never seem to get first answer, so I thought what I would do is simply take your code and do it the way I would do this, which is a lot neater if you don't mind me saying so:


<?php

function the_breadcrumb() {

$crumbholder = "<ul id='crumbs'>%s</ul>";
$linkedcrumb = "<li><a href='%s'>%s</a></li>";
$crumb = "<li>%s</li>";
$crumbs = array();

if (!is_home()) {

$crumbs[] = sprintf($crumb,site_url(),"Homepage");

if (is_category() || is_single()) {
$key = count($crumbs);
//using key will only give us one category link
foreach(get_the_category() as $category) {

if ($category->parent == 0) {
$parent_cat = $category;
}
else {
$child_cat = $category;
}
}

$crumbs[] = sprintf($linkedcrumb,get_category_link( $parent_cat->term_id ),$parent_cat->name);
$crumbs[] = sprintf($linkedcrumb,get_category_link( $child_cat->term_id ),$parent_cat->name);

if (is_single()) {

$crumbs[] = sprintf($crumb,get_the_title());

}

} elseif (is_page()) {

$crumbs[] = sprintf($crumb,get_the_title());

}
printf($crumbholder,implode("/",$crumbs));

}

elseif (is_tag()) {single_tag_title();}

elseif (is_day()) {echo"<li>Archive for "; the_time('F jS, Y'); echo'</li>';}

elseif (is_month()) {echo"<li>Archive for "; the_time('F, Y'); echo'</li>';}

elseif (is_year()) {echo"<li>Archive for "; the_time('Y'); echo'</li>';}

elseif (is_author()) {echo"<li>Author Archive"; echo'</li>';}

elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<li>Blog Archives"; echo'</li>';}

elseif (is_search()) {echo"<li>Search Results"; echo'</li>';}

echo '<div class="clear"></div></ul>';

}

2013-08-17

Arnav Joy answers:

try this

<?php

function the_breadcrumb() {

echo '<ul id="crumbs">';

if (!is_home()) {

echo '<li><a href="';

echo get_option('home');

echo '">';

echo 'Homepage';

echo "</a><span>/</span></li>";

if (is_category() ) {

echo '<li>';

the_category('<span>/</span></li><li>');



} else if (is_single()) {

echo '<li>';

$category = get_the_category( get_the_ID() );
if( $category[0] && $category[0]->parent != 0 ){
echo '<span>/</span></li><li>'.get_cat_name($category[0]->parent).'</li>';
}

the_category('<span>/</span></li><li>');



echo "<span>/</span></li><li>";

the_title();

echo '</li>';



}elseif (is_page()) {

echo '<li>';

echo the_title();

echo '</li>';

}

}

elseif (is_tag()) {single_tag_title();}

elseif (is_day()) {echo"<li>Archive for "; the_time('F jS, Y'); echo'</li>';}

elseif (is_month()) {echo"<li>Archive for "; the_time('F, Y'); echo'</li>';}

elseif (is_year()) {echo"<li>Archive for "; the_time('Y'); echo'</li>';}

elseif (is_author()) {echo"<li>Author Archive"; echo'</li>';}

elseif (isset($_GET['paged']) && !empty($_GET['paged'])) {echo "<li>Blog Archives"; echo'</li>';}

elseif (is_search()) {echo"<li>Search Results"; echo'</li>';}

echo '<div class="clear"></div></ul>';

}