您在这里有几个选项:
taxonomy-{$taxonomy}-{$term->parent}-{$term}.php
我们可以通过创建自己的层次结构来创建自己的层次结构(
或实际扩展现有的层次结构taxonomy-{$taxonomy}-{$term->parent}-{$term}.php
查看子术语时使用的模板。我们还将利用taxonomy_template
筛选以将新的分类法模板添加到层次结构中,以便使用它们。您可以尝试以下操作:(NOTE: 所有代码都未经测试,并对所有代码进行了注释,以便于后续操作和理解。代码还假设顶级术语已经具有taxonomy-{$taxonomy}-{$term}.php
模板
)
add_filter( \'taxonomy_template\', function ( $template )
{
// Get the current term object being viewed
$current_term = get_queried_object();
// We can restrict this to a single taxonomy, for example
// if ( $current_term->taxonomy !== \'my_taxonomy\' )
// return $template;
/**
* Check if current term is top level, if so, return the default $template which
* should be template taxonomy-{$taxonomy}-{$term}.php if found
*/
if ( $current_term->parent == 0 ) // Top level terms have parent of 0
return $template;
// We made it to here, so the term is not top level
// We need to get the top level term of the current term
$hierarchy = get_ancestors( $current_term->term_id, $current_term->taxonomy );
// The parent ID will always be the last ID in the array returned by get_ancestors
$parent_ID = end( $hierarchy );
// Now we can get the top level term object
$top_level_term = get_term_by( \'id\', $parent_ID, $current_term->taxonomy );
/**
* Lets build our custom template name, add subfolder name if template
* is in a subfolder, for example /subfolder/name-of-template.php
*/
$custom_template = \'taxonomy-{$current_term->taxonomy}-{$top_level_term->slug}-{$current_term->slug}.php\';
// Check if our custom template exist, if not, return default $template
$locate_template = locate_template( $custom_template );
if ( !$locate_template )
return $template;
// Finally, everything checked out, return our custom template
return $template = $locate_template;
});
您可以根据需要调整代码
自定义模板零件可以创建taxonomy-{$taxonomy}-{$term}.php
模板和利用循环中的模板部分,根据子术语包括模板部分。这里最好的方法是编写一个自定义函数,然后在循环内调用该函数(或任何需要它的地方),而不是get_template_part()
为此,我们需要调用模板部分,如下所示:
顶级术语将为{$term}。php,比如africa.php
子术语将是{$term->parent}-{$term}.php
喜欢africa-cameroon.php
默认/回退模板content.php
. 记住不要通过.php
函数模板名称的一部分
这是代码
/**
* Function to set template parts according to child term
*
* @param (string) $default Default template part to use like content.php
* @return $template
*/
function get_custom_template_part( $default = \'\' )
{
// Check if we have a value for $default, if not, return false
if ( !$default )
return false;
// Sanitize the $default value
$default = filter_var( $default, FILTER_SANITIZE_STRING );
// Check if we are on a taxonomy page, if not, return the $default template
if ( !is_tax() )
return get_template_part( $default );
// Get the current term being viewed
$current_term = get_queried_object();
/**
* Set our custom variables
* $top_level_term will hold the top level term object
* $part will hold the current term slug if the current term is not top level
*/
$top_level_term = \'\';
$part = \'\';
// Check if current term is top level, if not, get the top level parent
if ( $current_term->parent != 0 ) {
// We need to get the top level term of the current term
$hierarchy = get_ancestors( $current_term->term_id, $current_term->taxonomy );
// The parent ID will always be the last ID in the array returned by get_ancestors
$parent_ID = end( $hierarchy );
// Now we can get the top level term object
$top_level_term = get_term_by( \'id\', $parent_ID, $current_term->taxonomy );
$part = $current_term->slug;
}
// We now will set our template\'s name accordingly
if ( $top_level_term ) {
$name = $top_level_term->slug;
} else {
$name = $current_term->slug;
}
// We will now check if our template parts exist, if not, return our default
if ( $part ) { // This means we have a child term
$template = get_template_part( $name, $part );
} else { // This means top level term
$template = get_template_part( $name );
}
if ( $template )
return $template;
return get_template_part( $default );
}
您可以根据需要对此进行扩展,也可以在子文件夹中添加模板部分,然后只需将子文件夹名称附加到$name
函数内部现在,您可以在taxonomy-{$taxonomy}-{$term}.php
或taxonomy-{$taxonomy}.php
样板
默认模板:content.php
get_custom_template_part( \'content\' );