Last week I wrote about creating a breadcrumb trail, explaining how to do this for your front page, static Page and single post page. This leaves us with the category page and archives (day, month and year).
Category Page
The category page is quite similar to the single post page code that we saw last week, except that we use the single_cat_title() function along with the get_cat_id function to get the category ID of the page we’re on (get_the_category() wouldn’t work for category pages, as it’ll pick up all categories for the first post on the page and can potentially mess things up!) ie.
[sourcecode language="php"]if (is_category()) :
$pdata = get_cat_id( single_cat_title(“”,false) );
$data = get_category_parents($pdata, TRUE, ‘ » ‘);
$trail .= ” » “.substr($data,0,-9);
endif;[/sourcecode]
The Archives
The archive pages can be for a day, month or year, so we need to cover all 3 potential options. Note, to reduce a database call I’ve used paths relative to the root so you may need to edit these if your WordPress install is within a directory.
Day Archives
For the Day archives we want it in the format of Year » Month » Day, with the year and month being linked. To do this we can use the following code:
[sourcecode language="php"]if (is_day()) :
$trail .= ” » “.get_the_time(‘Y’).”“;
$trail .= ” » “.get_the_time(‘F’).”“;
$trail .= ” » “.get_the_time(‘l’);
endif;[/sourcecode]
Here we check if the archive page is a daily archive page, if so then we use the tag get_the_time() to get the information about the archives. This tag accepts the various PHP Date formatting to determine what to display. Here I’ve used the Y to get the full 4 digit year eg. 2009, then for the month archive link I’ve used Y/m which would insert 2009/02 into our link. I’ve set the month anchor text however to use ‘F’ which will display the name in full.
Finally I’ve then set the day to display as its name in full, using the ‘l’ (lowercase L). This isn’t linked as you’re already on the page for this
This will then give an output such as
Home » 2009 » February » Sunday
Month and Year Archives
For the Month and Year Archives we just adapt the above code for the day, slowly removing the additional ‘crumb’ at the end of the output, so for these two archives we use
[sourcecode language="php"]if (is_month()) :
$trail .= ” » “.get_the_time(‘Y’).”“;
$trail .= ” » “.get_the_time(‘F’);
elseif (is_year()) :
$trail .= ” » “.get_the_time(‘Y’);
endif;[/sourcecode]
Final Code
So our final function code, including the code from last week, should now be:
[sourcecode language="php"]function write_breadcrumb() {
$pid = $post->ID;
$trail = “Home“;
if (is_front_page()) :
// do nothing
elseif (is_page()) :
$bcarray = array();
$pdata = get_post($pid);
$bcarray[] = ” » “.$pdata->post_title.”\n”;
while ($pdata->post_parent) :
$pdata = get_post($pdata->post_parent);
$bcarray[] = ” » “.get_the_time(‘Y’).”“;
$trail .= ” » “.get_the_time(‘F’).”“;
$trail .= ” » “.get_the_time(‘l’);
elseif (is_month()) :
$trail .= ” » “.get_the_time(‘Y’).”“;
$trail .= ” » “.get_the_time(‘F’);
elseif (is_year()) :
$trail .= ” » “.get_the_time(‘Y’);
endif;
return $trail;
}
[/sourcecode]
The above code uses a right angled quote to separate the links however the most suitable markup for this is to use a list (except WordPress didn’t want me to paste that into the post!) so if you would rather use a list you can download the complete function, and just insert it into your functions.php file.
Remaining Pages
You may think, what about the other pages – tags, author pages and search results. I’ve not done this as the only page ‘above’ them is the home page, however if you wanted to add them in then you could use the correct conditional for each and just extend the if else statement above. These conditionals are is_tag(), is_author() and is_search().







Great stuff! Just what I needed. Coding is really not my strong area. Ty!
thank you very much..its your site that made me a real and succesful blogger.Now i understood more about blog templates and also i am learning again a more from this site. I will be very greatful to you if you pls vist my blog and suggest recommendations to enhance my blog and earn revenue from it…
What are your feelings on the overall usefulness of breadcrumbs. Some say it helps, some say it's actually more confusing.
Dennis, it depends on the complexity and type of site. If your site goes quite deep category/page wise, then it can be quite handy to have a breadcrumb trail, but of course it needs to be displayed in a way to prevent confusion.
if your site is just a couple of levels deep and mainly blog orientated then possibly a breadcrumb trail isn't worth it. I would possibly put them on my pages on my own site, but no where else as I only have top level categories. However a friend of mine runs a busy news based site and this is perfect for his needs.
I think it comes down to personal preference, but yes, displaying them well is a major point, so you don't confuse people
Good point. I have sub categories and even a sub sub category here and there. I may need to look further into this, thanks.
I simply use the breadcrumb plugin available from mtekk.weblogs.us.