使用WooCommerce以短代码获取按产品类别的产品 [英] Get Products by Product category in a shortcode with WooCommerce

查看:97
本文介绍了使用WooCommerce以短代码获取按产品类别的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a shortcode, I would like to get all the products from a specific Woocommerce category.

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"]

但是它会返回所有类别的所有产品,尽管在​​短代码中提供了类别。

But it returns all the products from all categories despite providing the category in the shortcode.

我该如何

谢谢

推荐答案

'product_cat'=> $ product_cat, ,您应该这样使用 tax_query

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

所以您的代码应该类似于(一点您的代码)

So your code should be something like (I have revisited a bit your code):

// 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"]






相关答案:


Related answers:

  • Nested shortcode to display dynamically woocommerce products category
  • Display a random product thumbnail for a product category in WooCommerce

这篇关于使用WooCommerce以短代码获取按产品类别的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆