为某些产品类别创建不同的模板文件 - Wordpress/Woocommerce? [英] Creating a different template file for certain Product Categories - Wordpress/Woocommerce?
问题描述
我正在尝试根据列出某个 Woocommerce 产品的类别制作不同的模板文件.
I am trying to make different template file depending on the category a certain Woocommerce product is listed in.
我无休止地在 Google 上搜索以找到解决方案.我尝试创建一个名为taxonomy-product_cat-[slugnamehere].php"的模板,但没有奏效.我尝试制作模板文件,然后使用 if/else 语句在 single_product.php 中设置模板文件.它也不起作用.
I have Googled endlessly to find a solution. I have tried creating a template with the name "taxonomy-product_cat-[slugnamehere].php" and it didn't work. I have tried making making a template file, then, using if/else statements setting the template file in single_product.php. It isn't working either.
我的产品的类别称为盒子".下面是来自 single_product 的代码;它似乎不起作用.任何建议将不胜感激:
The category for my product is called 'box'. Below is the code from single_product; it isn't seeming to work. Any advice would be appreciated:
} else if ( is_product_category() ) {
if ( is_product_category( 'box' ) ) {
woocommerce_get_template_part( 'content', 'single-product-sample-box-4');
}
}
else {
woocommerce_get_template_part( 'content', 'single-product');
}
我希望,如果选择了box"类别,则要显示 content-single-product-sample-box-4 模板 - 而只显示 content-single-product 模板.
I want, if the category is 'box' is selected, for the content-single-product-sample-box-4 template to be displayed - instead only the content-single-product template is being displayed.
推荐答案
我找到了答案!对于其他为此苦苦挣扎的人,我将发布我的解决方案...我只是想更改一个产品的模板(它是一个样品盒,因此与所有其他项目不同).
I found an answer! For anyone else struggling with this I will post my solution... I was only trying to change the template for ONE product (it was a sample box and therefore different from all other items).
取而代之的是:
} else if ( is_product_category() ) {
if ( is_product_category( 'box' ) ) {
woocommerce_get_template_part( 'content', 'single-product-sample-box-4');
}
}
else {
woocommerce_get_template_part( 'content', 'single-product');
}
我是这样做的:
} else if ( get_the_ID() == [PAGE ID GOES HERE] ) {
woocommerce_get_template_part( 'content', 'single-product-sample-box-4');
}
else {
woocommerce_get_template_part( 'content', 'single-product');
}
这篇关于为某些产品类别创建不同的模板文件 - Wordpress/Woocommerce?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!