我正在建立一个新网站,我对 Woocommerce 非常满意。我只需要一个快速技巧来获取每个类别中的产品数量。我已经调出了每个产品的类别,但无法弄清楚如何从该类别中获取产品数量。
我有一个适合我的产品的列表样式(实际上是活动网站的活动)。查看图像 https://dl.dropboxusercontent.com/u/13502930/mac.png.
我只想回显该类别旁边的“活动”计数。这就是我获取类别的方式:
echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' );
我尝试使用以下方法获取计数:
$numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'");
echo $numposts;
但它正在回响一些奇怪的数字。我尝试了该查询的一些变体,例如询问产品等。
[update]
这就是我能够做的:
<li><?php
$cat1 = $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' );
echo $cat1;
/*
$args = array( 'taxonomy' => 'product_cat' );
$terms = get_terms('product_cat', $args);
echo count($terms);
*/
$args = array( 'post_type' => 'product', 'taxonomy' => $cat1[0] );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo count( $loop->post->ID )
endwhile;
wp_reset_query(); // Remember to reset
?></li>
但它实际上以“1”为增量计算出所有类别中的所有产品......因此,它不是回显“类别:abc 有“3”个产品”,而是回显“类别:abc 有“1 1 1 1 1 1 1” ”
我知道我可以在这里做一个简单的过滤器,我感觉我就在那里。