I’ve made a custom function that retrieves the custom taxonomy term for a list of posts in a query, and echos both the name and the link. Additionally, it also excludes listing a term if I specify the term id (I have a special term, which I don’t want displayed, that I use as a loop hook).
This is achieved by this function
function my_get_the_term_list( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
foreach ( $terms as $term ) {
if(!in_array($term->term_id,$exclude)) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) )
return $link;
$term_links[] = '<a href="' . $link . '" rel="tag" class="category-link" title="See all stories from '.$terms_as_text.'">' . $term->name . '</a>';
}
}
$term_links = apply_filters( "term_links-$taxonomy", $term_links );
return $before . join( $sep, $term_links ) . $after;
}
I then display it with this line
<?php echo my_get_the_term_list( $post->ID, 'sports_category', '', ' ', '', array(3623) ); ?>
All well and good.
Now I am deeper into my site tree and I would like to retrieve the term/s of the current post once again, but now (as I am on the archive page for the term in above explanation) I only want to retrieve the sub-term/s and not display the parent term. I’ve tried modify the above, but I can’t seem to pull things apart.
The examples I’ve seen elsewhere don’t deal with custom terms, additionally I would like to form it into function so I can set some parameters as above.
This is my latest attempt
function my_get_the_children( $id, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {
$terms = get_term_children( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
foreach ( $terms as $term ) {
if(!in_array($term->term_id,$exclude)) {
$link = get_term_link( $term->name, $taxonomy );
if ( is_wp_error( $link ) )
return $link;
$term_links[] = '<a href="' . $link . '" rel="tag" class="category-link" title="See all stories from '.$terms_as_text.'">' . $term->name . '</a>';
}
}
$term_links = apply_filters( "term_links-$taxonomy", $term_links );
return $before . join( $sep, $term_links ) . $after;
}
Here’s more of a complete guide based on the
$wp_query
object:The Taxonomy
First you might want to know in which taxonomy you are, what its name is and retrieve all its available data from the object.
The Taxon/Term
Then you might want to do something with the current taxon/term.
The Ancestors/Parents
Getting the ancestors/parents offers a lot of possibilities. For example for a breadcrumb trail navigation or post meta data or simply to filter them out of the list of shown taxonomies.
Is it a hierarchical Taxonomy?
You always will have to distinguish between hierarchical (category like) and flat (post tags like) taxonomies.
Do we have Children, my dear?
Sometimes you’re in the middle of a really deeply nested hierarchical taxonomy. Then it makes sense to handle the children as well.