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.