首页 文章

在商店页面woocommerce上显示一个类别

提问于
浏览
-1

这就是问题:如何在woocommerce的商店主页上只显示一个类别?

2 回答

  • 1

    试一试只应显示“刀具”类别中的产品,将其更改为您的类别 .

    资源: - Exclude a specific category from shop page

    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( 'knives' ), // Display products in the knives category on the shop page
                    'operator' => 'IN'
                )));
    
            }
    
            remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
    
        }
    
  • 0

    它只显示一个类别的数据: -

    add_action( 'woocommerce_after_main_content', 'storefront_post_header_categories', 6 );  
    function storefront_post_header_categories() {  
    global $product;  
    $terms = get_the_terms( $product->get_id(), 'product_cat' );  
    if ($terms[0]->slug=='australian-open'){  
     dynamic_sidebar('accompanied-service');  
     }  
    }
    

相关问题