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:
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.
I know this is an old post but for some reason this code does not work anymore. It works fine listing the child terms on the parent taxonomy term, but on the child term archive page it just says ‘no categories’.
Maybe you have a fix for this?
Yeah, It isn’t working for me either. Did you find a fix for this yet?
try this one its only shown subcategories in the main category
get_queried_object_id(),
‘taxonomy’ => ‘YOUR-TAXONOY-SLUG’,
‘title_li’ => ”,
);
wp_list_categories($args);
?>
$args = array(
‘parent’ => get_queried_object_id(),
‘taxonomy’ => ‘YOUR TAXONOMY NAME’,
‘title_li’ => ”,
);
wp_list_categories($args);
Hello. Thank you for the code. It works.
But I have a question. How can I hide “title” when hovering the custom taxonomy links. At the moment, the description of each custom taxonomy is displayed in hovering title. Thank you.
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
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.
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!
Had been googling for a while for a solution like this. Cheers
Can it show thumbnail also for child terms ??
Thanks
@syedbalkhi I confused Taxonomy with Taxidermy…#TheAwkwardMoment
@rottenpeach_com lol