0

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

Category

--Category

---Category

----Category

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',);
                break;
                case 'level_2':
                get_template_part( 'taxonomy-content-level_2',);
                break;
                case 'level_3':
                get_template_part( 'taxonomy-content-level_3',);
                break;
                case 'level_4':
                get_template_part( 'taxonomy-content-level_4' );
                break;
            }

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

0

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!

1
  • 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
    Oct 3, 2017 at 17:36
0

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' );
  break;
  case 2:
  echo "Level 2";
  get_template_part( 'taxonomy-custom', 'level-2' );
  break;
  case 3:
  echo "Level 3";
  get_template_part( 'taxonomy-custom', 'level-3' );
  break;
  case 4:
  echo "Level 4";
  get_template_part( 'taxonomy-custom', 'level-4' );
  break;
  default:
  echo "Level others";
  get_template_part( 'taxonomy-custom', 'levels' );
}

Greetings

Your Answer

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

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