使用 WooCommerce 在短代码中按产品类别获取产品

Get Products by Product category in a shortcode with WooCommerce

我有一个简码,我想从特定的 Woocommerce 类别中获取所有产品。

add_shortcode( 'list-products', 'prod_listing_params' );
function prod_listing_params( $atts ) {
ob_start();

extract( shortcode_atts( array (
    'type' => 'product',
    'order' => 'date',
    'orderby' => 'title',
    'posts' => -1,
    'category' => '',
), $atts ) );

$options = array(
    'post_type' => $type,
    'order' => $order,
    'orderby' => $orderby,
    'posts_per_page' => $posts,
    'product_cat' => $product_cat,
);
$query = new WP_Query( $options );
if ( $query->have_posts() ) { ?>
    <div class="#">
        <?php while ( $query->have_posts() ) : $query->the_post(); ?>
        <p class="#">
        <span id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </span></p>
        <?php endwhile;
        wp_reset_postdata(); ?>
    </div>
<?php
    $myvar = ob_get_clean();
    return $myvar;
}
}

所以我使用简码:

[list-products category="shoes"]

但是 returns 所有类别的所有产品 尽管在短代码中提供了类别。

如何修改这个以按类别获取?

谢谢

而不是 'product_cat' => $product_cat, 你应该这样使用 tax_query

'tax_query' => array( array(
     'taxonomy' => 'product_cat',
     'field' => 'slug',
      'terms' => $atts['cat'],
) ),

所以你的代码应该是这样的 (我已经重新访问了你的代码):

// Creating a shortcode that displays a random product image/thumbail
if( !function_exists('prod_listing_params') ) {
    function prod_listing_params( $atts ) {
        ob_start();

         $atts = shortcode_atts( array (
            'type' => 'product',
            'order' => 'date',
            'orderby' => 'title',
            'posts' => -1,
            'category' => '', // category slug
        ), $atts, 'list_products' );

        $query = new WP_Query( array(
            'post_type' => $atts['type'],
            'order' => $atts['order'],
            'orderby' => $atts['orderby'],
            'posts_per_page' => $atts['posts'],
            'tax_query' => array( array(
                 'taxonomy' => 'product_cat',
                 'field' => 'slug',
                  'terms' => $atts['category'],
            ) ),
        ) );

        if ( $query->have_posts() ) { 
            ?>
                <div class="#">
                    <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                    <p class="#">
                    <span id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                    </span></p>
                    <?php endwhile;
                    wp_reset_postdata(); ?>
                </div>
            <?php
            $myvar = ob_get_clean();
            return $myvar;
        }
    }
    add_shortcode( 'list_products', 'prod_listing_params' );
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

用法示例:

[list_products category="shoes"]

相关回答:

您也可以使用 WooCommerce 本身提供的简码

[product_category category="appliances"]