WordPress Categories Breadcrumb Navigation

Background

I’m currently working on a custom WordPress Theme that uses category and sub-category archives extensively to create a document library.

I wanted to add breadcrumb navigation to the site to show the relationships between the categories and sub-categories and to add an extra level of usability to the site.

So here are the few lines of code I’ve written to create the category breadcrumb navigation:

<?php
$category = get_the_category();
$current_cat = $category[0]->cat_ID;
?>
<p class=”breadcrumb”> <a href=”<?php echo get_option(’home’); ?>/”>Home Page</a> &raquo; <?php echo get_category_parents($current_cat, TRUE, ‘ &raquo; ‘); ?> <?php the_title(); ?></p>

Note: This code must be added inside the Loop to ensure it works as intended. I’ve added this to both the archive.php - plus all category specific Template files - and single.php Template files.

Let’s look at the code a little closer.

Getting the Current Post’s Category

The first section looks like this:

<?php
$category = get_the_category();
$current_cat = $category[0]->cat_ID;
?>

This code uses the get_the_category() to get the category ID for the currently displayed category archive or post and passes it to a variable - $current_cat – for use later on when discovering the category hierarchy.

Creating the Hierarchy and Displaying the Breadcrumbs

The next section of code is used to create the hierarchy between the categories and to display the breadcrumb links to the user.

<p class=”breadcrumb”> <a href=”<?php echo get_option(’home’); ?>/”>Home Page</a> &raquo; <?php echo get_category_parents($current_cat, TRUE, ‘ &raquo; ‘); ?> <?php the_title(); ?></p>

The first part of the code:

<a href=”<?php echo get_option(’home’); ?>/”>Home Page</a>

simply adds a link back to the site’s home page.

The clever part of creating the category hierarchy and breadcrumbs is done by the get_category_parents() function. This creates a list of the parents categories of a category.

I’m passing 3 parameters to this function:

  • $current_cat: This is the category ID of the current post for which I want to return the parents.
  • TRUE: This ensures that each parent category is displayed as a link.
  • ‘ &raquo; ‘: This is the character I want to use to separate each category in the breadcrumbs.

The final part of the code looks like this:

<?php the_title(); ?>

If the users on a Post page this displays the title of the Post, otherwise it’s blank.

Pros & Cons

Using this code to display category breadcrumbs is a pretty straight forward way to add extra level of navigation to a WordPress powered site.

However, there are a few draw backs:

  1. It’s only category based navigation - If you want to add similar functionality for Pages and Sub-Pages additional code is needed.
  2. Posts can belong to only one category - This is a fundamental part of creating a hierarchical breadcrumb navigation: when writing a post it’s essential to only add it to only one category.
  3. Categories are always displayed as links followed by the separator character - This is great if you’re viewing a single Post; it’s exactly what you want - a link back up to the parent categories. But when viewing a category archive, it can look a little bit odd.

If you use this code and discover any other issues or any ways to improve it please feel free to add a comment below.

Dad About The Boy WordPress Theme

Dad: About the Boy WordPress Theme

Read More

7879 Designs Upgraded to WordPress 2.5

I know, I know.

It’s been an extremely long time since I last posted anything on 7879 Designs but not because 7879 is the latest in a long line of neglected ideas I’ve had.

In fact it’s been quite the oposite.

I’ve been pretty busy with work over the past few months so much so that I’ve only just got round to upgrading the 7879 Deisgns site to WordPress 2.5.

And as you can see, the good news is that the site is still here. The upgrade was as straight forward as ever without any errors in my pretty simple theme.

I’m slowly getting used to the new admin interface too. I’m still not convinced that all the changes are good ones - why put the Settings, Plugins and Users options all by them selves over on the right? - but I do like the redesign of the Write Post page.

Anyway, hopefully I’ll get around to posting more frequently now I’ve gone to the extreme length of upgrading and keep this site a little more up to date than it has been in the past.

That’s not a promise though.

The Humphrey Awards

The Humphrey Awards WordPress Theme

Read More

Just Rewards Blog WordPress Theme

Just Rewards Blog WordPress Theme

Read More

Hitting The Big Time WordPress Theme

Hitting the Big Time WordPress Theme

Read More

Newer Entries 

Older Entries