Skip to content

Instantly share code, notes, and snippets.

@benhuson
Created February 17, 2012 14:55
Show Gist options
  • Save benhuson/1853910 to your computer and use it in GitHub Desktop.
Save benhuson/1853910 to your computer and use it in GitHub Desktop.
get_the_term_list_breadcrumbs() - Same as get_the_term_list() but outputs a terms parents as part of the link.
/**
* Get the Term List Breadcrumbs
* Same as get_the_term_list() but outputs a terms parents as part of the link.
* Useful when you have many subcategories all with the same name.
*/
function get_the_term_list_breadcrumbs( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $breadcrumb_sep = ' → ' ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
foreach ( $terms as $term ) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) )
return $link;
// Find parents
$names = array();
$ancestors = get_ancestors( $term->term_id, $taxonomy );
if ( count( $ancestors ) > 0 ) {
foreach ( $ancestors as $anc ) {
$t = get_term( $anc, $taxonomy );
$names[] = $t->name;
}
}
$names[] = $term->name;
$link_text = implode( $breadcrumb_sep, $names );
$term_links[] = '<a href="' . $link . '" rel="tag">' . $link_text . '</a>';
}
$term_links = apply_filters( "term_links-$taxonomy", $term_links );
return $before . join( $sep, $term_links ) . $after;
}
@blickwert
Copy link

Ich have a little problem with this code I'm get getting following output:

<a href="$childtax_slug"> $parenttax_name - $childtax_name</a>

instead of

<a href="$parenttax_slug"> $parenttax_name </a> - <a href="$childtax_slug"> $childtax_name </a>

any idea how can I fix this?
Thanks Davelee

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment