Easy WordPress Breadcrumbs

Image by PetitPlat by sk_
Your site should always be as usable as possible and site navigation is a big part of that. Aside from your standard sidebars and top navigation, it’s always a good idea to include a breadcrumb to let users track their way back to the home page. Of course there are lots of plugins you can use to insert this, but why use a plugin, when you can do it just as easily on your own.
Here’s the code that you’ll need to add to your functions.php file:
//breadcrumb function
function YLcrumbs() {
if ((is_page() && !is_front_page()) || is_home() || is_category() || is_single()) {
echo '<ul id="breadcrumbs">';
echo '<li><a href="'.get_bloginfo('url').'">Home</a>»</li>';
$post_ancestors = get_post_ancestors($post);
if ($post_ancestors) {
$post_ancestors = array_reverse($post_ancestors);
foreach ($post_ancestors as $crumb)
echo '<li><a href="'.get_permalink($crumb).'">'.get_the_title($crumb).'</a>»</li>';
}
if (is_category() || is_single()) {
$category = get_the_category();
echo '<li><a href="'.get_category_link($category[0]->cat_ID).'">'.$category[0]->cat_name.'</a>»</li>';
}
if (!is_category())
echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
echo '</ul>';
}
}
Then, in your template, wherever you want to display your breadcrumb, you’d add the following:
<? YLcrumbs(); ?>
Note that the function generates a breadcrumb in a UL element, so your CSS might look something like this:
#breadcrumbs {padding: 10px 0;margin:0;}
#breadcrumbs li {display:inline;padding: 0 10px 0 0;}
That would give you a list of LI elements in a row. Then you can style it up further as you require.
Have you used this somewhere? Leave a link in the comments to your implementation of this breadcrumb system.



Hey thar! I love this solution, and I am most probably going to use it for all my wordpress sites from now on. Sick of plugins.
The first site I am testing it on now, is a site hosted locally (for testing) and the breadcrumbs linebreak after they exceed a total length of ca. 330px. Then the following permalink-name ends up below my div crumbs.
.crumb {background:#fff;margin-top:-10px;width:840px;height:25px;}
#breadcrumbs {font-size:10px;width:800px;padding-top:5px;padding-left:15px;}
#breadcrumbs ul {margin-left:0px;}
#breadcrumbs li {display:inline;padding: 0 2px 0 0;}
#breadcrumbs a{text-decoration:none !important;}
Thanks if you could help me figure out why this is.
Hi Mark,
Thanks for your function I am using it on two of my sites.
I noticed some problems using this function when category has subcategories.
On posts it just displays subcategory ignoring category.
On category it displays one of the subcategories instead of category…