Php Woocommerce按特定类别获取产品

Php Woocommerce按特定类别获取产品,php,wordpress,woocommerce,woothemes,Php,Wordpress,Woocommerce,Woothemes,我正在为woocommerce开发主题,我需要帮助按类别从产品中检索信息,例如, 我想显示“衬衫”类别的产品,限制为3项,以下是woo主题的代码,该代码按特色产品显示产品(我尝试更改为按类别显示,但不起作用) 用以下代码替换$args $args = array( 'post_type' => 'product', 'post_status' => 'publish', 'tax_query

我正在为woocommerce开发主题,我需要帮助按类别从产品中检索信息,例如, 我想显示“衬衫”类别的产品,限制为3项,以下是woo主题的代码,该代码按特色产品显示产品(我尝试更改为按类别显示,但不起作用)


    用以下代码替换$args

    $args = array(
            'post_type'             => 'product',
            'post_status'           => 'publish',
            'tax_query'             => array(
                array(
                    'taxonomy'      => 'product_cat',
                    'terms'         => array_map( 'sanitize_title', explode( ',', 'ENTER_CATEGORY' ) ),
                    'field'         => 'slug',
                    'operator'      => $atts['operator']
                )
            )
        );
    
    只需将“输入类别”一词替换为要显示的类别名称即可


    让我知道这是否满足您的要求。

    要按特定类别获取特色产品,您只需使用特色设置为true且您指定类别的wc_get_产品。请参阅下面的代码

    $prod_categories = array(10, 27);
    $product_args = array(
        'numberposts' => $limit,
        'post_status' => array('publish', 'pending', 'private', 'draft'),
        'post_type' => array('product', 'product_variation'),
        'orderby' => 'ID',
        'suppress_filters' => false,
        'order' => 'ASC',
        'offset' => 0
    );
    
    if (!empty($prod_categories)) {
        $product_args['tax_query'] = array(
            array(
                'taxonomy' => 'product_cat',
                'field' => 'id',
                'terms' => $prod_categories,
                'operator' => 'IN',
        ));
    }
    
    $products = get_posts($product_args);
    
    <?php
    
    // Get featured products by category. on this case its "shirts" which is the slug of the category.
    $query_args = array(
        'featured' => true,  
        'category' => array( 'shirts' ),
    );
    $products = wc_get_products( $query_args );
    
    
    
    <?php
    
    // Get featured products by category. on this case its "shirts" which is the slug of the category.
    $query_args = array(
        'featured' => true,  
        'category' => array( 'shirts' ),
    );
    $products = wc_get_products( $query_args );