[英]Count WP_Query returns 0 in Wordpress
尝试在Wordpress中实现自定义分页,但我无法获得动态创建分页元素的帖子数。
$loop = new WP_Query(
array(
'post_type' => 'product',
'post_status ' => 'publish',
'orderby' => 'post_date',
'order' => 'date',
'posts_per_page' => $per_page,
'offset' => $start,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $suv_cates
)
)
)
);
$count = new WP_Query(
array(
'post_type' => 'product',
'post_status ' => 'publish',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $suv_cates
)
)
)
);
return $count->post_count;
带$loop
的第一个查询返回我需要的帖子。 但是当我返回$count
或$count->post_count
,它返回0。
每页的帖子数设置为-1。
你可以算循环吗?
您需要重置第一个查询。 因此,在第一个循环和查询后添加此代码
wp_reset_postdata();
wp_reset_query();
然后让我知道结果。 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.