我有以下层次结构的Woocommerce产品类别:
Parent X
- child X1
- child X2
Parent Y
- child Y1
- child Y2
我在每个子类别中都有产品,没有任何产品有超过一个子类别。
我需要展示parent Y
以不同于Parent X
我试图创造taxonomy-product_cat-parent-x.php
和taxonomy-product_cat-parent-y.php
但没用
我在中找到了解决方案this article 对于类别,我在注释中找到了下面的分类代码,但作者认为它未经测试,我无法使用:
/**
* Filter the taxonomy hierarchy to inject a parent level of templates.
*
* @param string $template The current template.
* @return string Filtered taxonomy template.
*/
function new_tax_hierarchy( $template ) {
$term = get_queried_object();
// If not an object, or the object doesn\'t have a taxonomy, bail.
if ( ! is_object( $term ) || ! isset( $term->taxonomy ) )
return $template;
$taxonomy = $term->taxonomy;
// If the taxonomy isn\'t hierarchical, bail.
if ( ! is_taxonomy_hierarchical( $taxonomy ) )
return $template;
$templates = array();
$parent_id = $term->parent;
if ( 0 == $parent_id ) {
// Use default values from get_taxonomy_template().
$templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
$templates[] = "taxonomy-$taxonomy.php";
$templates[] = \'taxonomy.php\';
} else {
$parent = get_term( $parent_id, $taxonomy );
// Current templates.
$templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
$templates[] = "taxonomy-$taxonomy.php";
// Parent templates.
$templates[] = "taxonomy-$taxonomy-{$parent->slug}.php";
$templates[] = "taxonomy-$taxonomy-{$parent->term_id}.php";
$templates[] = \'taxonomy.php\';
}
return locate_template( $templates );
}
add_filter( \'taxonomy_template\', \'new_tax_hierarchy\' );
最合适的回答,由SO网友:Ali Basheer 整理而成
我解决了这两个问题:
1-使用template_include
在过滤器中,而不是taxonomy_template
2-我根据需要对这些行重新排序。
// Current first
$templates[] = "category-{$category->slug}.php";
$templates[] = "category-{$category->term_id}.php";
// Parent second
$templates[] = "category-{$parent->slug}.php";
$templates[] = "category-{$parent->term_id}.php";
$templates[] = \'category.php\';
这是我的最终代码:
/**
* Filter the taxonomy hierarchy to inject a parent level of templates.
*
* @param string $template The current template.
* @return string Filtered taxonomy template.
*/
function new_tax_hierarchy( $template ) {
$term = get_queried_object();
// If not an object, or the object doesn\'t have a taxonomy, bail.
if ( ! is_object( $term ) || ! isset( $term->taxonomy ) )
return $template;
$taxonomy = $term->taxonomy;
// If the taxonomy isn\'t hierarchical, bail.
if ( ! is_taxonomy_hierarchical( $taxonomy ) )
return $template;
$templates = array();
$parent_id = $term->parent;
if ( 0 == $parent_id ) {
// Use default values from get_taxonomy_template().
$templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
$templates[] = "taxonomy-$taxonomy.php";
$templates[] = \'taxonomy.php\';
} else {
$parent = get_term( $parent_id, $taxonomy );
// Parent templates.
$templates[] = "taxonomy-$taxonomy-{$parent->slug}.php";
$templates[] = "taxonomy-$taxonomy-{$parent->term_id}.php";
$templates[] = \'taxonomy.php\';
// Current templates.
$templates[] = "taxonomy-$taxonomy-{$term->slug}.php";
$templates[] = "taxonomy-$taxonomy.php";
}
return locate_template( $templates );
}
add_filter( \'template_include\', \'new_tax_hierarchy\' );