0

I have new custom Taxonomy for cars list spec. I would like to know what is the best way to create a different template for parent(main) and second layer and the third layer children taxonomies.

I can not use this way taxonomy-{taxonomy}-{slug}.php, Because I have a lot of category and subcategory names.

First there is the Brands page (main tax):

BMW
Benz
Volvo
And..

If I choose BMW for example. there is the Series page (second layer):

Series 1
Series 2
Series 3
And...

And if, for example, I choose series 3, I will enter the page of Generations of this series (third layer):

E21
E21 facelift
E30
And..

And if I choose E21 for example:

I enter the specifications page of this car (my new post-type).

Url: mysite{dot}com/cars/bmw/3-series/e21

I want each of the layers of the main category, the second layer, and the third layer to have a separate template.

Update: In this answer we can seperate children and parent. How I can do this for the second layer and the third layer children?

2 Answers 2

0

The {$type}_template_hierarchy filter can be used to add templates to the list of files to check for each request:

Filters the list of template filenames that are searched for when retrieving a template to use.

With this filter, you can do something like below: add to theme's functions.php or an mu-plugin (untested):

add_filter( 'taxonomy_template_hierarchy', static function ( $templates ) {
    $term = get_queried_object();

    if ( 'car-brand' !== $term->taxonomy ) {
        return $templates;
    }

    $ancestors = get_ancestors( $term->term_id, 'car-brand', 'taxonomy' );

    $new_template = sprintf( 'car-brand-ancestors-%d.php', count( $ancestors ) );

    return array( $new_template ) + $templates;
} );

With this approach, templates would apply like so:

Top-level Term: car-brand-ancestors-0.php
└─ Child Term: car-brand-ancestors-1.php
   └─ Grandchild Term: car-brand-ancestors-2.php

I use Query Monitor to check the template hierarchy, and I would recommend doing so here, as you'll probably want to adjust to prioritize more specific templates first.

2
  • I replaced category with car-brand and created taxonomy-car-brand-0.php but it show me my index page and not working.
    – och
    Jul 1, 2023 at 8:07
  • I have made changes to the answer to use car-brand custom taxonomy. Please try. Recommend installing Query Monitor plugin to confirm the filter is working; and please make sure to use the correct filename.
    – Caleb
    Jul 1, 2023 at 16:20
-1

Solution: This is worked fine:

In may taxonomy-MyTaxName.php file:

<?php
$term = get_term_by('slug', get_query_var('term'), 'MyTaxName');
$ancestors = get_ancestors( $term->term_id, 'MyTaxName', 'taxonomy' );
if (count( $ancestors ) == 0) {
  get_template_part('MyTaxName', '1');
}
if (count( $ancestors ) == 1) {
  get_template_part('MyTaxName', '2');
}
if (count( $ancestors ) == 2) {
  get_template_part('MyTaxName', '3');
}
?>

And I created 3 files:

  1. MyTaxName-1.php

  2. MyTaxName-1.php

  3. MyTaxName-1.php

3
  • This is effectively a copy of my answer. 👎
    – Caleb
    Jul 1, 2023 at 16:49
  • @Caleb Your answer not working!
    – och
    Jul 6, 2023 at 17:47
  • That's fine, but your answer is clearly derived from my answer, which means a comment acknowledging that it got you to a working solution.
    – Caleb
    Jul 6, 2023 at 19:05

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.