【发布时间】:2017-03-03 11:57:56
【问题描述】:
我只想在 /../shop 页面上列出两个特定类别的产品。其余所有内容都需要使用我在网站上已有的按钮进行搜索或找到。
我尝试了一些不同的代码 sn-ps,但似乎没有什么能达到我想要的效果。
任何帮助都会得到很多学徒, 非常感谢 刘易斯
【问题讨论】:
-
您的代码在哪里。显示您的代码。
标签: php wordpress woocommerce categories product
我只想在 /../shop 页面上列出两个特定类别的产品。其余所有内容都需要使用我在网站上已有的按钮进行搜索或找到。
我尝试了一些不同的代码 sn-ps,但似乎没有什么能达到我想要的效果。
任何帮助都会得到很多学徒, 非常感谢 刘易斯
【问题讨论】:
标签: php wordpress woocommerce categories product
WooCommerce 页面上有说明,您只需根据需要进行更改。
https://docs.woocommerce.com/document/exclude-a-category-from-the-shop-page/
在您的情况下,将运算符更改为“IN”和
terms => array('knives')
到
terms => 数组('your-cat-slug-1', 'your-cat-slug-2'),
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
if ( ! is_admin() && is_shop() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'your-cat-slug-1' , 'your-cat-slug-2' ), // Display products in the your-cat-slug-1/your-cat-slug-2 category on the shop page
'operator' => 'IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
将此代码放入您的 Child-themes functions.php 文件中。
【讨论】:
我了解到您希望在 /shop 页面上仅显示特定类别,对吗?因此,使用下面的代码,将 product_cat 更改为您需要的...
add_action('pre_get_posts','shop_filter_cat');
function shop_filter_cat($query) {
if (!is_admin() && is_post_type_archive( 'product' ) && $query->is_main_query()) {
$query->set('tax_query', array(
array ('taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'type-1'
)
)
);
}
}
【讨论】: