【发布时间】: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