rss

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>&raquo;</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>&raquo;</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>&raquo;</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.

2 Comments

  1. 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.

  2. 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…

Leave a Reply

About The WordPress Guru

Over the last 4 or 5 years, I’ve been digging into the Wonders of WordPress. From the days when the Classic theme was the shizz until now, WordPress has gone from strength to strength both as the blogging platform of choice for many big players and as a CMS with...

Learn more »