SarahCreate a Breadcrumb Trail II

Written by Sarah from Stuff By Sarah

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.

if (is_category()) :
	$pdata = get_cat_id( single_cat_title("",false) );
	$data = get_category_parents($pdata, TRUE, ' » ');

	$trail .= " » ".substr($data,0,-9);
endif;

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:

if (is_day()) :
	$trail .= " &raquo; <a href='/".get_the_time('Y')."/'>".get_the_time('Y')."</a>";
	$trail .= " &raquo; <a href='/".get_the_time('Y/m')."/'>".get_the_time('F')."</a>";
	$trail .= " &raquo; ".get_the_time('l');
endif;

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

if (is_month()) :
	$trail .= " &raquo; <a href='/".get_the_time('Y')."/'>".get_the_time('Y')."</a>";
	$trail .= " &raquo; ".get_the_time('F');
elseif (is_year()) :
	$trail .= " &raquo; ".get_the_time('Y');
endif;

Final Code

So our final function code, including the code from last week, should now be:

function write_breadcrumb() {
	$pid = $post->ID;
	$trail = "<a href='/'>Home</a>";

	if (is_front_page()) :
		// do nothing
	elseif (is_page()) :
		$bcarray = array();
		$pdata = get_post($pid);
		$bcarray[] = " &raquo; ".$pdata->post_title."\n";
		while ($pdata->post_parent) :
			$pdata = get_post($pdata->post_parent);
			$bcarray[] = " &raquo; <a href='".get_permalink($pdata->ID)."'>".$pdata->post_title."</a>\n";
		endwhile;
		$bcarray = array_reverse($bcarray);
		foreach ($bcarray AS $listitem) :
			$trail .= $listitem;
		endforeach;
	elseif (is_single()) :
		$pdata = get_the_category($pid);
		$data = get_category_parents($pdata[0]->cat_ID, TRUE, ' &raquo; ');

		$trail .= " &raquo; ".substr($data,0,-8);
	elseif (is_category()) :
		$pdata = get_cat_id( single_cat_title("",false) );
		$data = get_category_parents($pdata, TRUE, ' &raquo; ');

		$trail .= " &raquo; ".substr($data,0,-9);
	elseif (is_day()) :
		$trail .= " &raquo; <a href='/".get_the_time('Y')."/'>".get_the_time('Y')."</a>";
		$trail .= " &raquo; <a href='/".get_the_time('Y/m')."/'>".get_the_time('F')."</a>";
		$trail .= " &raquo; ".get_the_time('l');
	elseif (is_month()) :
		$trail .= " &raquo; <a href='/".get_the_time('Y')."/'>".get_the_time('Y')."</a>";
		$trail .= " &raquo; ".get_the_time('F');
	elseif (is_year()) :
		$trail .= " &raquo; ".get_the_time('Y');
	endif;

	return $trail;
}

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().

Follow this blogger on Twitter!

Sarah Written by Sarah from Stuff By Sarah
Posted on March 1st, 2009 and filed under WordPress Coding & Design
Do not forget to subscribe to our RSS feed for updates
  • Digg This Post
  • Tweet This Post
  • Stumble This Post
  • Submit This Post To Delicious
  • Submit This Post To Reddit
  • Submit This Post To Mixx
  • BloggingTips Uses Aweber

6 Responses to “Create a Breadcrumb Trail II”

Author comments are in a darker gray color for you to easily identify the posts author in the comments

  1. seo says:

    Great stuff! Just what I needed. Coding is really not my strong area. Ty!

  2. abdul vajid says:

    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…

  3. Dennis Edell says:

    What are your feelings on the overall usefulness of breadcrumbs. Some say it helps, some say it’s actually more confusing.

  4. Sarah says:

    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 :)

  5. Dennis Edell says:

    Good point. I have sub categories and even a sub sub category here and there. I may need to look further into this, thanks. :)

  6. Dean Saliba says:

    I simply use the breadcrumb plugin available from mtekk.weblogs.us. :)

Trackbacks

Comments are closed.

Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums

Subscribe To BloggingTips Via RSS Subscribe To Blogging Tips Via Email Follow Us On Twitter Follow us on Facebook Find Out More About Our Newsletter

Sponsors

Blogging Tips Newsletter

Our WordPress Themes

 

Our Free E-Books