Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Display Child Taxonomy on Parent Taxonomy’s Archive Page

Last updated on by
Elegant Themes
How to Display Child Taxonomy on Parent Taxonomy’s Archive Page

In the past we have shown you how to display subcategories on category pages in WordPress. Recently while working with Custom Taxonomies, we found a need to display child-taxonomies on parent-taxonomies archive page. After doing a bit of research, we didn’t find a single tutorial covering this issue. In this article, we will show you how to display a list of child taxonomies on taxonomies pages.

Open up your custom taxonomy template file which may look like: taxonomy-{taxonomyname}.php and paste the following code where ever you want to display the list:

<?php 
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); 
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);	
}
?>

Don’t forget to replace YOUR-TAXONOMY-NAME with the name of your taxonomy.

Final Result:

List of Taxonomies

Explanation:

We are using get_term_by to query the information of the current taxonomy by slug. For example if your taxonomy is called topics and you are on a page /topics/nutrition/ then $term variable will pull all the data related to the specific term page that you are on.

In the project we were working on, the topics taxonomy was hierarchical just like categories. So we decided to run a conditional using $term->parent variable. This variable outputs the ID of the parent taxonomy. So if you are on the taxonomy nutrition which is the parent taxonomy, then $term->parent will echo 0. This is why we said if $term->parent == 0 then use wp_list_categories() function to display terms from our custom taxonomy that are child_of the term which page you are on. We accomplished this by using $term->term_id as the child_of variable.

Now if you go to the child taxonomy page, it would have been blank because the $term->parent would no longer equals to 0. On a child taxonomy page, $term->parent outputs the ID of the parent category. So we ran an else statement using the same wp_list_categories() function except we changed $term->term_id to $term->parent.

There you have it. We hope that this helps everyone who was looking for a solution.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • Susan Clifton

    I’m using the plugin Taxonomy Images so I would like to include that thumbnail in this list. This is my structure:
    Custom post type= Artwork
    hierarchical taxonomy= Artist Name

    I’m looking for each artist name to up with the thumbnail as a grid list to view artwork.
    I created a taxonomy template for the single artist page, no problem but can’t figure out how to do a list of artists. This solution almost works for me. But I’m getting each artist multiple times in this list.

    Hint…Hint ;)

    • http://www.wpbeginner.com Editorial Staff

      Hey Susan,

      Not sure why you are getting each artist multiple times. As for Taxonomy Images, Michael Fields have a bunch of queries on the plugins page.

  • Jake

    Almost exactly what I was looking for. I however have one problem: when my Taxonomy has no children I get a single list item that says “No Categories”. What should I do to just display nothing if there are no children?

    Much appreciated!

  • James

    Had been googling for a while for a solution like this. Cheers

  • Rasha

    Can it show thumbnail also for child terms ??
    Thanks

  • Rottenpeach_com

    @syedbalkhi I confused Taxonomy with Taxidermy…#TheAwkwardMoment

    • syedbalkhi

      @rottenpeach_com lol