I'm looking for a solution to dynamically create different templates for each category level.





I thought of something to get the taxonomy current level ID, to use a function like below, but I did not find a solution.

I have not found anything that returns me level or a unique ID for each level in a taxonomy

$term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
echo $term->cat_level_id;

switch ($current_level_id) {
                case 'level_1':
                get_template_part( 'taxonomy-content-level_1',);
                case 'level_2':
                get_template_part( 'taxonomy-content-level_2',);
                case 'level_3':
                get_template_part( 'taxonomy-content-level_3',);
                case 'level_4':
                get_template_part( 'taxonomy-content-level_4' );

I know that wordpress understands taxonomy-custom-slug.php, but it's not something I can use because it's not feasible to create templates since there are many categories / sub.

Thanks in advance for your attention.

2 Answers 2


While I do not know of a method already implementing this, a simple approach could be to count the amount of (grand)parents like so:

function get_level($id=NULL, $taxonomy='') {
   if ( ! $id) {
     $id = get_queried_object_id();

   $term = get_term($id, $taxonomy, OBJECT);
   if ($term === NULL)
     return 0;

   if ($term->parent == 0)
     return 1;

   return get_level($term->parent, $taxonomy) + 1;

Which will return 1 for each term without a parent, 2 for "first level", etc.

You can use this directly in get_template_part(). In the example $hierarchical_taxonomies is an array of taxonomies you want to use these special templates for.

$hierarchical_taxonomies = array('post_tag', 'my_tax');
if (in_array(get_query_var('term'), $hierarchical_taxonomies)) {
  get_template_part('taxonomy/level', get_level(NULL, get_query_var('term')));

This will try to load taxonomy/level-1.php, taxonomy/level-2.php, .. Make sure these files exist!

  • Thanks Kero for your answer, somehow it works, but for my use I got something different, below I left my piece of solution. Thank you
    – Infolu
    Commented Oct 3, 2017 at 17:36

I took some time to think about this issue and I was able to put together a clean solution.

Below is my piece of code explained.

This worked well for me.

//Search taxonomy current category
$term = get_term_by( 'slug', get_query_var('term'), get_query_var('taxonomy') );
//Search taxonomy current parents category
$level = get_category_parents( $term->term_id, true, ',' );
//print_r( $levels );
//$str = 'test, test, test, test,,';
$level_count = substr_count($level, ',');
switch ($level_count) {
  case 1:
  echo "Level 1";
  get_template_part( 'taxonomy-custom', 'level-1' );
  case 2:
  echo "Level 2";
  get_template_part( 'taxonomy-custom', 'level-2' );
  case 3:
  echo "Level 3";
  get_template_part( 'taxonomy-custom', 'level-3' );
  case 4:
  echo "Level 4";
  get_template_part( 'taxonomy-custom', 'level-4' );
  echo "Level others";
  get_template_part( 'taxonomy-custom', 'levels' );


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.