For a while now I have been swapping plugins to try and get a breadcrumb navigation system going on this blog. There are many of these available but I could not find one that does what I wanted it to do. Finally I gave up and wrote my own little function to do the job.
If you are interested the entire process is actually very simple. Firstly you will need the following function in your functions.php. Your theme might not have such a file but then you can simply create one and add the following code to it:
<?php
function breadcrumb() {
echo 'You are here: ';
if(is_home()) {
echo '<strong>Home</strong> ';
} else {
echo '<a href="';
echo get_option('home');
echo '">';
echo 'Home ';
echo "</a> » ";
if (is_page()) {
echo '<strong>';
echo the_title();
echo '</strong>';
} elseif (is_tag()) {
echo '<strong>';
echo 'Archives for the ';
single_tag_title();
echo ' tag';
echo '</strong>';
} elseif (is_category()) {
echo '<strong>';
echo 'Archives for the ';
single_cat_title();
echo ' category';
echo '</strong>';
} elseif (is_day()) {
echo '<strong>';
echo 'Archive for ';
the_time('F jS, Y');
echo '</strong>';
} elseif (is_month()) {
echo '<strong>';
echo 'Archive for ';
the_time('F, Y');
echo '</strong>';
} elseif (is_year()) {
echo '<strong>';
echo 'Archive for ';
the_time('Y');
echo '</strong>';
} elseif (is_author()) {
echo '<strong>';
echo 'Author Archive';
echo '</strong>';
} elseif (isset($_GET['paged']) && empty($_GET['paged'])) {
echo '<strong>';
echo 'Blog Archives';
echo '</strong>';
} elseif (is_search()) {
echo '<strong>';
echo 'Search Results';
echo '</strong>';
} elseif (is_single()) {
the_category(' » ');
if (is_single()) {
echo " » ";
echo '<strong>';
the_title();
echo '</strong>';
}
}
}
}
?>
Now all you need is to call this function on your theme pages wherever you want the breadcrumbs to appear. In my case it was in header.php. The following is all you need to do this:
<div id="breadcrumb">
<p><?php breadcrumb(); ?></p>
</div><!-- close breadcrumb -->
That’s it, that is all that is required. I have tested the function and it works but testing for all scenarios has not been done e.g. this blog does not have an authors page to test that portion of the code. I also do not use tags on this blog.
You can have a look at the top of this page, just under the menu, to see this code in action.
I hope I have covered all the possibilities. If I slipped-up on a type of page that could exist please let me know. I have included a section for a search results page. On this part of the navigation I simply show ’search results’. I would like to include the actual search string in here somewhere but I could not find a function to do this with. If you know of such a function kindly let me know as well.
Well, that is at least one less plugin that I have to activate on my blog. I hope you can find a use for this.
Till next time.
UPDATE: 24 November 2008 10H35
This code does work (it is actually running on this page) but there could be a problem with categories if you have parent categories set up.
As is, the code will show all the selected categories for the relevant article. This includes the parent category if it has also been checked in WordPress. It will however not show the parent category if it has not been checked. If this suits your requirements, this code is all you need.
If you however need the parent category shown automatically as well you will need a function added to this code. This function is reasonably complicated and should rather be in a plugin. If this is what you need I would recommend the Yoast Breadcrumbs plugin. You could alternatively wait for my new theme to be released, there I have this function built in.
Things could get even more complicated. I could choose two categories for an article each with a separate parent, or I could select 2 sub categories both under the same parent. My code nor the plugin mentioned above will handle these scenarios. I could be mistaken, but I do not think there are plugins that have got this one right. This might be something for the plugin authors to take further.
My thanks to Ben for pointing this problem out.











Good effort Lyndi, but I don’t think it’s going to work for pages or categories that are nested deeper than the first level. This adds some complexity.
FYI, Injader has breadcrumbs built into the core. When you have a chance to check it out, I’m more than happy to show you where the breadcrumb code is.
Ben, it actually does work, check out the navigation on this page, this article is nested within 3 categories. All possibilities will obviously have to be checked.
What I meant is if you have categories that are organised like this:
– Music
– - Classical
– - Easy Listening
– - Rock and Pop
– - – The Beatles
Then if you put a post in the category “The Beatles”, it should show Music – Rock and Pop – The Beatles – (post title), which shows the path through your categories to the post.
What you’ve done is to use breadcrumbs to show that a post has multiple categories, which in my view is incorrect because each of those categories is technically at the top level. So if I were to navigate to this post I wouldn’t actually click on Home – Markup – Tips – WordPress – Breadcrumb Navigation. I’m not sure if multiple categories should be shown as breadcrumbs.
OK, now I am with you. On this article Wordpress is the top category with tips and markup as sub-categories. The links are only showing in the navigation because all 3 have been checked. This is wrong, I agree, but the problem lies with the the_category function WordPress makes available, there seems to be no way to return the tree structure. This could only have an effect on categories as tags and pages cannot be nested. You might just have hit on something here, the available plugins for breadcrumbs all have this same problem. Will investigate and get back to you.
Ben, I just found a WordPress function called get_category_parents. It looks promising. Will have a look later. You might just have helped to improve this code quite significantly. Thanks.
I’ve got it, or at least most of it. I get it to show the parent category without it being selected but then with only the first category. This is fine though, it is better than just showing the selected categories. Will update the code later. Gotta rush.
Lyndi, If I look at the navigation path at the top of this page, it shows Home-Markup-Tips-WordPress. Is this because this topic is posted in all these categories?
Yes, that is what it means.
Damn! This is what I was looking for! Thank you very much!! In the past I’ve used a plugin but it didn’t worked out as I wanted it.. so I had to uninstall the plugin!
Anyway.. I’m going to use it soon, when I’m going to change some things in my themes..
Please keep an eye on this post, this code could change soon. At present the code does however do everything the current plugins do.
I do not think breadcrumbs are good fit for WordPress. I actually cut breadcrumbs from my theme.
There are too many different navigational paths and with multiply categories it becomes a mess as noted above.
I do not think there is something as too much navigation possibilities. I know people who will navigate only via breadcrumbs if it is available. One strives to cater for as many as possible.
This code does not mess up multiple categories, it actually shows them all. What it does not do is show the parents of a category automatically. With Ben’s suggestions I might just be able to include this soon.
Very slick, Lyndi, very slick indeed.
Your blog is just getting betterer and betterer.
Thanks Richard, I assume that is better, betterer, betterest.
Way to go, I like because it isn’t a plugin and it is easy to install. Plus, since you support it, it is even better.
Thanks.