I don’t fully understand what you’re asking but here’s my guess…
Say you have hierarchical taxonomy (e.g. category) with the following term tree:
- News (ID: 1)
- World (ID: 2)
- Africa (ID: 5)
- Asia (ID: 6)
- Europe (ID: 7)
- Business (ID: 3)
- Science (ID: 4)
- World (ID: 2)
Now you visit /category/news/world/africa/
WordPress will look for the following templates: category-africa.php
or category-5.php
but neither of those exist so WordPress falls back to category.php
or archive.php
.
If you’re asking “Can WordPress work up the tree to the first existing template?” then no, it cannot. But you can implement this functionality with the following…
/**
* Recurisively check if a term or term parent has a specific template
*
* @param WP_Term $term
* @return ?string
*/
function parent_term_template( WP_Term $term ) {
/**
* Find template by pattern: taxonomy_name-term_slug.php || taxonomy_name-term_id.php
*
* @link https://developer.wordpress.org/reference/functions/locate_template/
* @var ?string
*/
$template = locate_template( "{$term->taxonomy}-{$term->slug}.php" ) ?: locate_template( "{$term->taxonomy}-{$term->term_id}.php" );
/**
* If template exists, return it
*/
if ( $template ) {
return $template;
}
/**
* Else, check if parent template exists
*/
else if ( !empty( $term->parent ) ) {
return parent_term_template( get_term_by( 'id', $term->parent, $term->taxonomy ) );
}
/**
* No template
*/
return null;
}
/**
* Filter the path of the current template before including it
*
* @link https://developer.wordpress.org/reference/hooks/template_include/
*/
add_filter( 'template_include', function( string $template ) {
/**
* Apply to all taxonomies
*/
if ( is_category() || is_tag() || is_tax() ) {
/**
* Get term specific template location
*
* @var string
*/
$found = parent_term_template( get_queried_object() );
/**
* If template exists, return it
*/
if ( $found ) {
return $found;
}
}
return $template;
} );
This will check if the current view is either a category, tag or custom taxonomy archive page. If so, it takes the queried term and passes it to a recursive function that looks for a matching template up the tree of parents until one is found.
For example, if we’re viewing /category/news/world/africa/
and there is no category-africa.php
template, this code will look for the direct parent template. In this case: category-world.php
and if that doesn’t exist it will move to category-news.php
and finally if that doesn’t exist, it will fall back to the default.
Still I have no idea if this answers your question but it might help somebody!