Breadcrumbs for Taxonomy Pages

This post has been marked as old. The code might no longer work. Comments have been disabled as this is no longer maintained. Use the search on the right to find a newer tutorial.

I’m working on a project right now that uses hierarchical taxonomies, and the client requested a breadcrumb along the top. I searched online but couldn’t find any good existing plugins, so thought I’d write my own.

Note: This was built for WordPress 3.03. WordPress 3.1 (hopefully coming out this month) will add a lot of features to taxonomies, including hierarchical URLs. This solution is NOT designed for hierarchical URLs. If you read through the code you’ll see I’m assembling them with 3 things:

  • bloginfo('url') – This is the URL for your site
  • $item->taxonomy – This is the name of the taxonomy the term is in
  • $item->name – This is the name of the taxonomy term

Since WordPress doesn’t currently support hierarchical URLs for taxonomies, no matter how deep your term is it will still have the url structure of yoursite.com/taxonomy-name/taxonomy-term. Hopefully 3.1 will give us a URL from get_term_by(), but it doesn’t now, which is why I did it this way.

With that said, here’s the code. Place this in your functions.php file (or custom_functions.php for Thesis):

<?php
/* Taxonomy Breadcrumb */
function be_taxonomy_breadcrumb() {
// Get the current term
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
// Create a list of all the term's parents
$parent = $term->parent;
while ($parent):
$parents[] = $parent;
$new_parent = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ));
$parent = $new_parent->parent;
endwhile;
if(!empty($parents)):
$parents = array_reverse($parents);
// For each parent, create a breadcrumb item
foreach ($parents as $parent):
$item = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ));
$url = get_bloginfo('url').'/'.$item->taxonomy.'/'.$item->slug;
echo '<li><a href="'.$url.'">'.$item->name.'</a></li>';
endforeach;
endif;
// Display the current term in the breadcrumb
echo '<li>'.$term->name.'</li>';
}
view raw gistfile1.aw hosted with ❤ by GitHub

To use it in your theme, simply call be_taxonomy_breadcrumb() within a <ul>

WordPress Development

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk

Reader Interactions

Comments

  1. fano says

    Petty good! But two things missing:
    – Homepage as the first element of the list
    – Right arrow after each element (except current page)
    Good job!

    • Bill Erickson says

      You can modify the code to present it that way if you want. The above solution is how my client wanted it, so that’s what I’m sharing.