WordPress – Custom Taxonomy Breadcrumbs

Taken from: http://www.free-php.net/780/wordpress-custom-taxonomy-breadcrumbs/

While developing a theme that uses custom taxonomies and custom post types, I ran across the need to include breadcrumb navigation to the custom taxonomies. After a couple of days of searching around and not finding any real solutions, I ended up creating my own function to do it.

Simply copy this to your functions.php file:


<?php

function postTypeCrumbs($postType, $postTax) {

$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
 $taxonomy = get_taxonomy($term->taxonomy);
 $parents = get_ancestors( $term->term_id, $postTax );
 $parents = array_reverse($parents);
 $archive_link = get_post_type_archive_link( $postType );

echo '<ul class="ax_crumbs">';

if ($taxonomy) {
 echo '<li><a href="' . $archive_link . '" title="' . $taxonomy->labels->name . '">' . $taxonomy->labels->name . '</a> &raquo; </li>';
 }

foreach ( $parents as $parent ) {
 $p = get_term( $parent, $postTax );
 echo '<li><a href="' . get_term_link($p->slug, $postTax) . '" title="' . $p->name . '">' . $p->name . '</a> <span>&raquo;</span> </li>';
 }

if ($term) {
 echo '<li>' . $term->name . '</li>';
 }

echo '</ul>';

}
?>

Paste this where ever you would like the taxonomy to display.


<?php postTypeCrumbs('post type', 'taxonomy name'); ?>

Revisions

Tags: ,

No comments yet.

Leave a Reply