我已经使用了以下代码多年了,直到我升级到WooCommerce版本3.1.1。
该函数简单地将默认存档product.php更改为基于定义的WooCommerce类别slug的替代模板。
我已经阅读了更改日志,我认为有几件事可能与我的问题有关,但我不确定,因此为什么要在这里联系:(
Changelog对我的相关说明似乎是:
修复 - 添加了woocommerce_output_product_categories替换 woocommerce_product_subcategory函数以防止过时的主题 来自商店和类别输出类别的模板文件 err。
中的页面
但是,在上一个版本3.3.0中,对WooCommerce的默认布局/主题进行了许多更改,这也可能是错误的。
对此的任何帮助或指导都将很棒。
add_filter( 'template_include', 'wpse138858_woocommerce_category_archive_template' );
function wpse138858_woocommerce_category_archive_template( $original_template ) {
if ( is_product_category( array( 'cat-1', 'cat-2' ) ) )
{
return get_stylesheet_directory().'/woocommerce/archive-product_no_sidebar.php';
}
elseif ( is_product_category( array( 'cat-3', 'cat-4' ) ) )
{
return get_stylesheet_directory().'/woocommerce/archive-product_sidebar.php';
}
elseif ( is_product_category( array( 'cat-5', 'cat-6' ) ) )
{
return get_stylesheet_directory().'/woocommerce/archive-product_clubs_page.php';
}
else
{
return $original_template;
}
}
edit - 功能中有错误的错误,我的错,对不起...我已经直接从实时站点复制了代码它更可读
安装插件WP回滚,然后在WooCommerce上的"已安装插件"菜单中单击"回滚"。必须做同样的事情。
else
语句在您的elseif
语句中。您必须在这样的之后放置它:
add_filter( 'template_include', 'wpse138858_woocommerce_category_archive_template' );
function wpse138858_woocommerce_category_archive_template( $original_template ) {
// If cat 1,2,3 return template archive product set 1
if(is_product_category(array('cat-1','cat-2','cat-3'))) {
return get_stylesheet_directory().'/woocommerce/archive-product_set_1.php';
// If cat 4,5,6 return template archive product set 2
}elseif(is_product_category(array('cat-4','cat-5','cat-6'))) {
return get_stylesheet_directory().'/woocommerce/archive-product_set_2.php';
// Else return original template
}else {
return $original_template;
}
}