Use category_template Filter to automatically set a specific category template for a subcategory
During this time a website was developed, inside the website, there are two levels of categorization for articles, where the parent category is a separate template, and a sub-category template is used for each secondary category below the parent category, and each parent and sub-category template is different. The most straightforward way to accomplish this is to follow the official WordPress tutorial and use a template file like “category-slug.php” to specify a template for each category. While achievable, this is an ugly implementation that would result in copying an identical template for each subcategory. When adding subcategories, you'd need to copy it again, creating a lot of repetitive and pointless work, and making it harder to maintain later.
Automatically assign subcategory templates via category_template Filter
According to years of WordPress development experience, we intuitively believe that there must be a better way to achieve, so we did some research and testing, found that you can use category_template Filter to achieve this requirement is very simple, the following is the code to achieve this requirement. First get the current category, if the parent category of the current category is a specified category, try to get the specified category template, if the category template exists, return the specified category template, if it does not exist, return the default template.
add_filter( 'category_template', function ( $template )
{
$term = get_queried_object();
if ( $term->parent == 58 ) {
$new_template = locate_template( [ 'templates/archive/archive-topic.php' ] );
if ( '' != $new_template ) {
return $new_template;
}
}
return $template;
}, 99 );
The above code sets the templates/archive/archive-topic.php file as the category template for the sub-category whose parent category is "58".
What if you need to set a template for a subcategory whose parent category is "68"? Just copy the above if logic and modify "58" and "archive-topic.php". Well, while the implementation at the beginning of the article is a bit better, it still requires copying the code, and there is some risk in using the category ID as the logic for specifying the category.
Better implementation methods, better scalability and stability
Our goal is to be the domesticThe most professional WordPress service providerWhat we're after is not only to fulfill the requirement, but to do so in the most concise, efficient and extensible way, and in a way that is easy for the customer to use. So one step further, we transformed the above code. First of all, get the top level category of the current category, if the top level category exists, try to find the corresponding "category-top level category slug.php" file of the top level category in the "templates/category/" directory, if the file exists, return this template as the second level category template, if it doesn't exist, return to the default template.
add_filter( 'category_template', function ( $template )
{
$term = get_queried_object();
$ancestors = get_ancestors( $term->term_id, 'category' );
if ( ! empty( $ancestors ) ) {
$ancestors = get_term( $ancestors[ 0 ] );
$new_template = locate_template( [ 'templates/category/category-'. $ancestor->slug .' .php' ] );
if ( '' ! = $new_template ) {
return $new_template;
}
}
return $template; }
}, 99 );
In this way, it is much more convenient to increase the classification, directly add a "category-top category slug.php" can be set to sub-category template, and in the migration of data, as long as the classification Slug does not change, there is no need to modify the code to adapt the data, the operation method and the WordPress default method of specifying the template is similar. If you need it, you can modify the above code according to your own needs. Or if you have a better implementation, you can contribute to facilitate more friends in need.