【问题标题】:Get Products by Product category in a shortcode with WooCommerce使用 WooCommerce 在简码中按产品类别获取产品
【发布时间】:2018-02-07 01:55:16
【问题描述】:

我有一个简码,我想获取特定 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"]

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

如何修改它以按类别获取?

谢谢

【问题讨论】:

    标签: php wordpress woocommerce categories shortcode


    【解决方案1】:

    您应该这样使用tax_query,而不是'product_cat' =&gt; $product_cat,

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

    相关答案:

    【讨论】:

    • 完美!谢谢
    【解决方案2】:

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

    [product_category category="appliances"]
    

    【讨论】:

    • 是的,我遇到的唯一问题是,我的主题开始提供自己的风格,而且我将对其进行更改以在其旁边显示产品变体,而无需进入新页面。但你的回答是欢呼!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-06
    • 2016-01-09
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多