admin管理员组文章数量:1287486
I tried tplacing this:
<?php $count = count($custom_posts); ?>
<h2><?php echo $count; ?></h2>
at the end of the loop:
<?php if ( bbp_get_forum_title() == 'Test Forum 1' ) : ?>
<?php $custom_posts = new WP_Query(); ?>
<?php $custom_posts->query('post_type=blocks&location=Business and Finance&order=DESC'); ?>
<?php while ($custom_posts->have_posts()) : $custom_posts->the_post(); ?>
<div class="ad">
<?php the_content(); ?>
</div>
<?php $count = count($custom_posts); ?>
<h2><?php echo $count; ?></h2>
<?php endwhile; ?>
<?php endif; ?>
But instead of the total of posts, I getting this output:
Translation 1
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim 1
Any suggestions to fix this?
I tried tplacing this:
<?php $count = count($custom_posts); ?>
<h2><?php echo $count; ?></h2>
at the end of the loop:
<?php if ( bbp_get_forum_title() == 'Test Forum 1' ) : ?>
<?php $custom_posts = new WP_Query(); ?>
<?php $custom_posts->query('post_type=blocks&location=Business and Finance&order=DESC'); ?>
<?php while ($custom_posts->have_posts()) : $custom_posts->the_post(); ?>
<div class="ad">
<?php the_content(); ?>
</div>
<?php $count = count($custom_posts); ?>
<h2><?php echo $count; ?></h2>
<?php endwhile; ?>
<?php endif; ?>
But instead of the total of posts, I getting this output:
Translation 1
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim 1
Any suggestions to fix this?
Share Improve this question asked Aug 28, 2011 at 13:34 wycwyc 3,90719 gold badges61 silver badges97 bronze badges3 Answers
Reset to default 132Correct way of getting the total number of posts is:
<?php $count = $custom_posts->found_posts; ?>
http://codex.wordpress/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, while found_posts
is the count for all available posts that meets the requirements of the query without pagination. Thank you for the correction.
Manny linked correct documentation page but post_count
is wrong.
To get total number of posts WP_Query
returns use "found_posts"
<?php
// The Query
$query = new WP_Query( $args );
$total = $query->found_posts;
Or this:
$query = new WP_Query( $args );
$count = $query->post_count;
本文标签: Counting the posts of a custom Wordpress loop (WPQuery)
版权声明:本文标题:Counting the posts of a custom Wordpress loop (WP_Query)? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741232242a2362321.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论