Php 2个商业商店的布局
我必须要命名为“收藏”和“商店”的类别,我想要的是两个类别的孩子不同的布局 我已经用模板_include函数尝试过了,如下所示:Php 2个商业商店的布局,php,wordpress,woocommerce,wordpress-theming,Php,Wordpress,Woocommerce,Wordpress Theming,我必须要命名为“收藏”和“商店”的类别,我想要的是两个类别的孩子不同的布局 我已经用模板_include函数尝试过了,如下所示: function lab5_template_include( $template ) { if ( category_has_children('collectie')) { $template = get_stylesheet_directory() . '/woocommerce/archive-product-collectie.php
function lab5_template_include( $template ) {
if ( category_has_children('collectie')) {
$template = get_stylesheet_directory() . '/woocommerce/archive-product-collectie.php';
}elseif ( is_product_category('shop')) {
$template = get_stylesheet_directory() . '/woocommerce/archive-product-shop.php';
}
return $template;
}
我该怎么做
编辑
我用来自的解决方案解决了它
我在taxonomy-product_cat.php中添加了下面几行
// We need to get the top-level category so we know which template to load.
$get_cat = $wp_query->query['product_cat'];
// Split
$all_the_cats = explode('/', $get_cat);
// How many categories are there?
$cat_count = count($all_the_cats);
//
// All the cats say meow!
//
// Define the parent
$parent_cat = $all_the_cats[0];
// Collectie
if ( $parent_cat == 'collectie' ) woocommerce_get_template( 'archive-product-collectie.php' );
// Shop
elseif ( $parent_cat == 'shop' ) woocommerce_get_template( 'archive-product.php' );
我认为最好运行
while
循环来确定父级类别名称。然后,您可以将其与要以不同方式显示的不同父类别进行匹配
编辑lab5\u模板_include()
应使用is\u product\u category()
而不是is\u product\u taxonomy()
,后者在产品类别和标签存档中返回true
function lab5_template_include( $template ) {
if( is_product_category()){
$slug = get_query_var('product_cat');
$cat = get_term_by( 'slug', $slug, 'product_cat' );
$catParent = so_top_product_category_slug($cat);
// locate the new templates
if ( 'collectie' == $catParent ) {
$new_template = locate_template( array( 'woocommerce/archive-product-collectie.php' ) );
} elseif ( 'shop' == $catParent ) ) {
$new_template = locate_template( array( 'woocommerce/archive-product-shop.php' ) );
}
// set the new template if found
if ( '' != $new_template ) {
$template = $new_template ;
}
}
return $template;
}
add_filter('template_include', 'lab5_template_include', 20 );
编辑修复了缺陷并提高了so\u top\u product\u category\u slug()的效率
。现在测试和工作
// get the top-level parent product category from a term object or term slug
function so_top_product_category_slug($cat) {
if( is_object( $cat ) ){
$catid = $cat->term_id;
} else {
$cat = get_term_by( 'slug', $cat, 'product_cat' );
$catid = $cat->term_id;
}
$parentId = $cat->parent;
$parentSlug = $cat->slug;
while ($parentId > 0) {
$cat = get_term_by( 'id', $parentId, 'product_cat' );
$parentId = $cat->parent; // assign parent ID (if exists) to $catid
$parentSlug = $cat->slug;
}
return $parentSlug;
}
谢谢你,海尔加,我会试试看,然后再回来!仅供参考-我已更新为使用
locate\u模板
。我认为这应该有帮助。虽然我还没有测试过so\u top\u product\u category\u slug()
函数,但它是根据我知道可以工作的另一个函数改编的,所以我对它寄予厚望。:)谢谢你的帮助,helga,我用1的解决方案解决了这个问题。你试过我的解决方案吗?没用吗?2.您应该在此处发布解决方案,这样您就可以接受它作为答案,尽管链接的支持线程甚至与筛选模板\u include
无关。这是一个完全不同的问题。嗨,赫尔加,上面的解决方案不起作用,我非常感谢你的帮助。我在评论中发布的链接符合我对“收藏品”和“商店”类别使用不同布局的需要。