计算自定义Wordpress循环(WP_Query)的帖子?
2 个回答
- 投票数
-
- 2011-08-28
获取帖子总数的正确方法是:
<?php $count = $custom_posts->found_posts; ?>
http://codex.wordpress.org/Class_Reference/WP_Query#Properties
编辑:确认@Kresimir Pendic的回答可能正确.
post_count
是该特定页面的帖子计数,而found_posts
是所有可满足查询要求且无分页的可用帖子的计数.谢谢您的纠正.Correct way of getting the total number of posts is:
<?php $count = $custom_posts->found_posts; ?>
http://codex.wordpress.org/Class_Reference/WP_Query#Properties
Edit: acknowledging @Kresimir Pendic's answer as probably correct.
post_count
is the count of posts for that particular page, whilefound_posts
is the count for all available posts that meets the requirements of the query without pagination. Thank you for the correction.-
谢谢!嘿,最后一个问题.我如何使用该数字来制作if语句,该语句不在该循环中(在循环之前).因为似乎数字仅在我将变量放在该循环之后才显示.Thanks! Hey one last question. How can I use that number to make an if statement which is out of that loop (before of the loop). Because it seems like the number is only displayed when I place the variable after that loop.
- 0
- 2011-08-28
- janoChen
-
您可以将$ count=$ custom_posts->post_count放在$ custom_posts-> query()之后.请注意,$ custom_posts->post_count仅会为您提供结果集的该"页面"中的结果数.如果需要获取"整个"结果集中的结果总数,请使用$ custom_posts->found_posts.You can put the $count = $custom_posts->post_count just after the $custom_posts->query(). Note that $custom_posts->post_count only gets you the number of results in that 'page' of the result set. If you need to get the total number of results in the 'whole' result set, use $custom_posts->found_posts.
- 4
- 2016-07-29
- Robert Durgin
-
在大多数情况下,此答案很可能不正确.使用found_posts(所有找到的帖子)而不是post_count(此页面上显示的帖子数).从逻辑上来说,此评论是多余的,但从社交角度而言,则不是.This answer is most likely not correct for most situations. Use found_posts (all found posts) instead of post_count (number of posts to display on this page). This comment is redundant logically speaking, but not socially speaking.
- 2
- 2017-12-23
- Herbert Van-Vliet
-
这个答案是不正确的.$ custom_posts->post_count`将返回此页面上显示的帖子数量,因此它将显示查询的`posts_per_page`值,或者如果显示的剩余数量较少,则显示较低的值. 正确的答案应该是使用$ custom_posts->found_posts的<@ kresimir-pendic>`的答案.This answer is incorrect. `$custom_posts->post_count` will return the amount of posts shown on this page, so it will display either the `posts_per_page` value of the query or a lower value if the amount remaining to show is lower. the correct answer should be `<@kresimir-pendic>`'s answer that uses `$custom_posts->found_posts`
- 1
- 2018-03-12
- Infinity Media
-
- 2017-11-02
Manny链接了正确的文档页面,但
post_count
错误. 要获取WP_Query
返回的帖子总数,请使用"found_posts"<?php // The Query $query = new WP_Query( $args ); $total = $query->found_posts;
Manny linked correct documentation page but
post_count
is wrong. To get total number of postsWP_Query
returns use "found_posts"<?php // The Query $query = new WP_Query( $args ); $total = $query->found_posts;
-
这个应该是公认的答案.This one should be the accepted answer.
- 3
- 2018-02-06
- Christine Cooper
-
这绝对是正确的答案.This is absolutely the right answer.
- 1
- 2018-03-12
- Infinity Media
-
我也确认这是正确的答案.这应该被接受.I also reconfirm that this the correct answer. This should be accepted.
- 0
- 2019-06-21
- I am the Most Stupid Person
-
我可以确认这一答案确实是正确的.如重新确认I can confirm the confirmation that this answer is in fact true. As is the re-confirmation
- 0
- 2020-01-30
- Bysander
-
在确认最新确认的确认中,我确定原始确认确实已确认,之后的确认也是如此.In confirming the confirmation of the most recent confirmation I have determined that the original confirmation is indeed confirmed, as is the confirmation after that one.
- 0
- 2020-08-18
- 38365
我尝试替换这个:
在循环结束时:
但是我得到的输出不是帖子的总数:
有什么建议解决此问题吗?