您可以使用template_include
钩子加载分类法的特定模板。您需要更改my-taxonomy
使用特定的自定义分类法,并将brand-subcategories.php
无论模板名称是什么。
/**
* Force Taxonomy Subcategories to new template
*
* @param String $template - Expected template path
*
* @return String $template
*/
function wpse303717_subcategory_template( $template ) {
// We\'re not on a taxonomy page
if( ! ( is_tax() && is_tax( \'my-taxonomy\' ) ) ) {
return $template;
}
// Grab the queried object, _should_ be a term but make sure.
$queried_object = get_queried_object();
// We either don\'t have the right object OR we\'re on a top level category becuase $term->parent === 0
if( isset( $queried_object->parent ) && empty( $queried_object->parent ) ) {
return $template;
}
// Our template could be located anywhere, we could store it in a subdirectory but
// we would need to specify a relative path from the theme root to it.
// Example: \'templates/brand-subcategories.php\'
if( false !== ( $new_template = locate_template( \'brand-subcategories.php\' ) ) ) {
$template = $new_template;
}
return $template;
}
add_filter( \'template_include\', \'wpse303717_subcategory_template\' );
如果您想加载每个子类别的特定模板,则需要对其进行测试
$queried_object->term_id
与您期望的相比。但请注意,您的客户可能会删除这些条款,这会使您的代码失效。