Is there a way to use different templates for different levels within a hierarchical taxonomy, specified by filename. I know about taxonomy-taxonomyname.php
and taxonomy-taxonomyname-term.php
templates. Looking for a more generic “per taxonomy level” based method.
So, a taxonomy named Earth might contain:
- Africa
- Asia
- Europe
- South America
- …
Is there a way to have one template for Africa, Asia, Europe and South America (earth-numbers-level.php
)… and another template for Cameroon, Congo, Japan, Greece… (earth-bullets-level.php
)?
Looking over a few different template hierarchy docs, I’m guessing not. If that’s true, what’s the best/most efficient way to differentiate taxonomy levels within taxonomy-earth.php
so that I might use different template parts for each?
+1ed Pieter Goosen’s answer, but this time I want to be the one who advocate the “make it simple” way.
In your taxonomy-earth.php
template, you can check if the queried term as a parent, and require another template if so.
If you ask this question, I guess you already have (or wish to create) 2 templates, let’s call them:
taxonomy-earth-continent.php
taxonomy-earth-country.php
In your taxonomy-earth.php
file you can:
<?php
$slug = 'taxonomy-earth';
// default to continent
$name="continent";
// maybe set specific template to "country" if queried term has a parent
$term = get_queried_object();
if ( is_object( $term ) && ! empty( $term->parent ) )
$name="country";
get_template_part( $slug, $name );
These 6 lines of template code, will be enough to require a different template for “child” terms.
Thanks to the use of get_template_part
the template code is also child-theme friendly and triggers an hook: get_template_part_taxonomy-earth
that can be used to do things when the template is required.