使用WooCommerce以短代码获取产品类别的产品

时间:2017-08-29 14:39:17

标签: php wordpress woocommerce categories shortcode

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

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

如何修改此类别以获取类别?

由于

2 个答案:

答案 0 :(得分: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"]

相关答案:

答案 1 :(得分:1)

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

[product_category category="appliances"]
相关问题