Taxonomy Templates… by hierarchical level?

+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.

Leave a Comment