我会在template_redirect
:
function wpse53892_taxonomy_template_redirect() {
// code goes here
}
add_action( \'template_redirect\', \'wpse53892_taxonomy_template_redirect\' );
然后,检查查询是否为自定义分类法:
if ( is_tax() ) {
// This query is a custom taxonomy
}
接下来,获取术语对象,并找出它是否有父对象:
// Get the queried term
$term = get_queried_object();
// Determine if term has a parent;
// I *think* this will work; if not see below
if ( 0 != $term->parent ) {
// Tell WordPress to use the parent template
$parent = get_term( $term->parent );
// Load parent taxonomy template
get_query_template( \'taxonomy\', \'taxonomy-\' . $term->taxonomy . \'-\' . $parent->slug . \'php\' );
}
// If above doesn\'t work, this should:
$term_object = get_term( $term->ID );
if ( 0 != $term_object->parent; {}
所以,把这一切放在一起:
function wpse53892_taxonomy_template_redirect() {
// Only modify custom taxonomy template redirect
if ( is_tax() ) {
// Get the queried term
$term = get_queried_object();
// Determine if term has a parent;
// I *think* this will work; if not see above
if ( 0 != $term->parent ) {
// Tell WordPress to use the parent template
$parent = get_term( $term->parent );
// Load parent taxonomy template
get_query_template( \'taxonomy\', \'taxonomy-\' . $term->taxonomy . \'-\' . $parent->slug . \'php\' );
}
}
}
add_action( \'template_redirect\', \'wpse53892_taxonomy_template_redirect\' );