简体   繁体   English

Woocommerce - 商店页面产品仅列出特定类别

[英]Woocommerce - Shop page product listing only certain category

I'm wanting to only have products from two certain categories listed on the /../shop page. 我想只在/../shop页面上列出两个特定类别的产品。 All the rest will need to be searched for or found using buttons I have already on the site. 所有其余的都需要使用我在网站上已有的按钮进行搜索或找到。

I've tried a few different code snippets but nothing seems to do what I'm wanting it to. 我尝试过几个不同的代码片段,但似乎没有什么可以做我想要的。

Any help would be much apprenticed, Many Thanks Lewis 任何帮助都会有很多学徒,非常感谢刘易斯

It's explained on the WooCommerce Page, you only need to change it to your needs. 它在WooCommerce页面上进行了解释,您只需根据需要进行更改即可。

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

In your case, change the operator to "IN" and 在您的情况下,将运算符更改为“IN”和

terms => array('knives') terms => array('knives')

to

terms => array( 'your-cat-slug-1' , 'your-cat-slug-2' ), terms => array('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' );

}

Put this Code in your Child-themes functions.php file. 将此代码放在Child-themes functions.php文件中。

I understand that you wish to show only specific categries on the /shop page correct? 我了解您希望仅显示/shop页面上的特定类别是否正确? So use the code below, change product_cat to what you need it to be... 因此,使用下面的代码,将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'
                                     )
                         )
           );   
        }
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM