Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

1 Answer 1

up vote 9 down vote accepted

Correct way of getting the total number of posts is:

<?php $count = $custom_posts->post_count; ?>

http://codex.wordpress.org/Class_Reference/WP_Query#Properties

share|improve this answer
    
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. –  janoChen Aug 28 '11 at 14:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.