首页 文章

Woocomerce:如何在商店页面上“不显示”商品分类

提问于
浏览
-1

我想不要在商店页面中显示所有类别名称为“鞋子”的产品 .

我的意思是在商店页面显示所有产品只有鞋类别名称,我会在另一页面上添加鞋子产品(我创建一个鞋页,并将添加所有的鞋子产品)所以请帮助我(我需要使用代码php或插件) .

谢谢

2 回答

  • 0

    它很容易工作,只需要在功能页面中编写一个功能 .

    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() && ! is_user_logged_in() ) {
    
    $q->set( 'tax_query', array(array(
    'taxonomy' => 'product_cat',
    'field' => 'slug',
    'terms' => array( 'shirt', 'tshirt', 'pant' ), //Category name which      not to want display products on the shop page
    'operator' => 'NOT IN'
    )));
    
    }
    
    remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
    
    }
    

    不想在商店页面上展示产品的类别名称('衬衫','t恤','裤子')

    只是你可以保存你的页面,然后检查,

    如果您看到显示任何错误,那么您可以从上面的代码列表中删除此代码 .

    remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
    
  • 1

    你可以使用woocommerce_product_query钩子,它与pre_get_posts非常相似,只不过它已经有了适当的条件逻辑 .

    add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
    
    function custom_pre_get_posts_query( $q ) {
    
    $tax_query = (array) $q->get( 'tax_query' );
    
    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'samples' ), 
           'operator' => 'NOT IN'
    );
    
    
    $q->set( 'tax_query', $tax_query );}
    

相关问题