http://www.suburban-glory.com/blog?page=170
/*
* based on http://snipplr.com/view/57988/
*/
function get_term_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) {
$chain = '';
$parent = &get_term($id, $taxonomy);
try {
if (is_wp_error($parent)) {
throw new Exception('is_wp_error($parent) has throw error ' . $parent->get_error_message());
}
}
catch (exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
// use something less drastic than die() in production code
//die();
}
if ($nicename) {
$name = $parent->slug;
} else {
$name = htmlspecialchars($parent->name, ENT_QUOTES, 'UTF-8');
}
if ($parent->parent && ($parent->parent != $parent->term_id) && !in_array($parent->parent, $visited)) {
$visited[] = $parent->parent;
$chain .= get_term_parents($parent->parent, $taxonomy, $link, $separator, $nicename, $visited);
}
if ($link) {
$chain .= '' . $name . '' . $separator;
} else {
$chain .= $parent->name . $separator;
}
return $chain;
}
function get_tag_id($tag) {
global $wpdb;
$link_id = $wpdb->get_var($wpdb->prepare("SELECT term_id FROM $wpdb->terms WHERE name = %s", $tag));
return $link_id;
}
function my_breadcrumb($id = null) {
echo '';
echo 'Home';
echo " > ";
if (is_category() || is_single() || is_tax()) {
if (is_single()) {
$cat = get_the_category_list(' > ', 'multiple');
// make sure uncategorised is not used
if (!stristr($cat, 'Uncategorized')) {
echo $cat;
echo ' > ';
}
echo '';
the_title();
echo '';
}
if (is_category()) {
$cat = get_category_parents(get_query_var('cat'), true, ' > ');
// remove last >
echo preg_replace('/>\s$|>$/', '', $cat);
}
if (is_tax()) {
$tag = single_tag_title('', false);
$tag = get_tag_id($tag);
$term = get_term_parents($tag, get_query_var('taxonomy'), true, ' > ');
// remove last >
echo preg_replace('/>\s$|>$/', '', $term);
}
} elseif (is_page()) {
if ($id != null) {
$an = get_post_ancestors($id);
if(isset($an['0'])) {
$parent = '' . ucwords(get_the_title($an['0'])) . '';
echo !is_null($parent) ? $parent . " > " : null;
}
$parent = get_the_title($id);
$parent = '' . ucwords($parent) . '';
echo !is_null($parent) ? $parent . " > " : null;
}
echo '';
ucwords(the_title());
echo '';
}
}