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.
// Taxonomy name
$taxonomy = get_query_var( 'taxonomy' );
// Taxonomy object
get_taxonomy( $taxonomy );
// Taxonomy name
get_taxonomy( $taxonomy )->label;
The Taxon/Term
Then you might want to do something with the current taxon/term.
// The current taxon/term slug
$term_slug = get_query_var( 'term' );
// The complete current taxon/term object
$term = get_term_by( 'slug', $term_slug, $taxonomy );
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.
// Ancestors/Parents
$ancestors = get_ancestors(
$term->term_id
,$taxonomy
);
foreach ( $ancestors as $ancestor )
{
// The currently looped parents/ancestors object
$ancestor_obj = get_term( $ancestor, $taxonomy );
// The currently looped parents/ancestors name
$ancestor_name = $ancestor_obj->name;
// Link to the parent/ancestor taxon/term archive
$ancestor_link = get_term_link( $ancestor, $taxonomy )
}
Is it a hierarchical Taxonomy?
You always will have to distinguish between hierarchical (category like) and flat (post tags like) taxonomies.
// Are we in a hierarchical taxonomy?
if ( is_taxonomy_hierarchical( $taxonomy ) )
{
// Do stuff
}
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.
// Based on the above retrieved data
$children = get_term_children(
$term->term_id
,$taxonomy
);
foreach ( $children as $child )
{
// The currently looped child object
$child_obj = get_term( $child, $taxonomy );
// The currently looped child name
$child_name = $child_obj->name;
// Link to the child taxon/term archive
$child_link = get_term_link( $child, $taxonomy );
}