Woocommerce - 仅限特定类别的商品页面产品列表

问题描述:

我只希望在/../shop页面上列出来自两个特定类别的产品。其余所有内容将需要被搜索或使用我已经在网站上的按钮找到。Woocommerce - 仅限特定类别的商品页面产品列表

我已经尝试了几个不同的代码片段,但似乎没有做我想要的东西。

任何帮助将非常学徒, 非常感谢 刘易斯

+0

你的代码在哪里?显示你的代码。 – Deep

我知道你希望显示仅在/shop页面正确的特定categries?因此,使用下面的代码,改变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' 
            ) 
         ) 
      ); 
     } 
    } 

它在WooCommerce页解释,你只需要将它改变您的需求。

https://docs.woocommerce.com/document/exclude-a-category-from-the-shop-page/

在你的情况,改变操作员 “IN” 和

术语=>数组( '刀')

术语=>数组(“你的'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的function.php文件中。

+0

小迟到回复对不起,但我已经测试了这个片段,我认为它做了我所需要的一切,然后意识到它不仅允许监听/商店页面内的某些产品类别,而且仅允许这些类别显示在搜索。有没有办法只在/商店页面上允许某些类别,但搜索仍会搜索所有产品。 –

+0

我不确定,现在不能测试它。但是,如果 (!is_admin()&&!is_search()&& is_shop()){ 可能有效。 – mbg