找产品类别产品与WooCommerce一个简码(Get Products by Product ca

2019-09-29 00:18发布

我有一个短代码,我想获得的所有产品从一个特定的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"]

但它返回全部来自所有类别的产品 ,尽管在短码提供的类别。

我怎么能修改此按类别获得?

谢谢

Answer 1:

而不是'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产品类别
  • 显示随机产品缩略图在WooCommerce产品类别


Answer 2:

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

[product_category category="appliances"]


文章来源: Get Products by Product category in a shortcode with WooCommerce