Family Encyclopedia >> Electronics

How to Display Child Taxonomies on Parent Taxonomy Archive Pages in WordPress

As seasoned WordPress developers, we've previously shared techniques for displaying subcategories on category pages. Recently, while building a site with custom taxonomies, we needed to list child terms on parent taxonomy archive pages. Surprisingly, we couldn't find a dedicated tutorial. In this guide, drawn from our hands-on experience, we'll walk you through the exact code to achieve this.

Open your custom taxonomy template file, such as taxonomy-{taxonomyname}.php, and insert the following code where you want the child list to appear:

<?php
$term = get_queried_object();
if( $term->parent == 0 ) {
    wp_list_categories( 'taxonomy=YOUR-TAXONOMY-NAME&depth=1&show_count=0&title_li=&child_of=' . $term->term_id );
} else {
    wp_list_categories( 'taxonomy=YOUR-TAXONOMY-NAME&show_count=0&title_li=&child_of=' . $term->parent );
}
?>

Replace YOUR-TAXONOMY-NAME with your actual taxonomy name.

End result:

How to Display Child Taxonomies on Parent Taxonomy Archive Pages in WordPress

How it works:

We use get_queried_object() to retrieve details of the current taxonomy term. For instance, on a page like /topics/nutrition/ where 'topics' is your taxonomy, $term holds all data for 'nutrition'.

Our project featured a hierarchical 'topics' taxonomy, similar to categories. We check $term->parent: if it's 0 (parent term), we call wp_list_categories() with child_of={$term->term_id} and depth=1 to list direct children.

On a child term page, $term->parent returns the parent's ID (not 0), so the else clause lists children of that parent using child_of={$term->parent}.

This reliable method has streamlined navigation in our custom taxonomy projects. We hope it solves your challenge too.