如何获取查询帖子中的行数,如mysql count(*)。
$obj_name = new WP_Query($args);
while ($obj_name->have_posts()) : $obj_name->the_post();
// here i want to predict looping counts
endwhile;
我怎样才能做到这一点。如何获取查询帖子中的行数,如mysql count(*)。
$obj_name = new WP_Query($args);
while ($obj_name->have_posts()) : $obj_name->the_post();
// here i want to predict looping counts
endwhile;
我怎样才能做到这一点。$num = $obj_name->post_count;
Reference: wp_query
这里公认的答案是错误的,这在我的情况下也得到了证实。请从引用页面进行比较:
$post_count 显示的帖子数。
$found_posts 找到的与当前查询参数匹配的帖子总数
这样,$post\\u count将显示,例如,如果有多页结果,则显示每页的帖子数。只有当总数小于每页的结果数时,它才会匹配总数。
获取总结果数的正确方法是:
$obj_name->found_posts
.
要获取WP\\U查询返回的帖子总数,请使用;found\\u posts“;
下面是一个例子-
<?php
$args = array(
\'post_type\' => \'post\'
);
$the_query = new WP_Query( $args );
$totalpost = $the_query->found_posts;
?>
使用自定义帖子类型名称代替“post”,也可以传递类别id(“cat”=>;4,)