繁体   English   中英

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

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

我想只在/../shop页面上列出两个特定类别的产品。 所有其余的都需要使用我在网站上已有的按钮进行搜索或找到。

我尝试过几个不同的代码片段,但似乎没有什么可以做我想要的。

任何帮助都会有很多学徒,非常感谢刘易斯

它在WooCommerce页面上进行了解释,您只需根据需要进行更改即可。

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

在您的情况下,将运算符更改为“IN”和

terms => array('knives')

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' );

}

将此代码放在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'
                                     )
                         )
           );   
        }
     }

暂无
暂无

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

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