0

I have loop and I want to get the amount of posts on a current page. So I try:

<?php $post_number = $wp_query->post_count; ?>
<?php echo($post_number) ; ?>
<?php while ( have_posts() ) : the_post();?>
    <?php get_template_part( 'post', get_post_format() );?>
<?php endwhile; ?>

but it doesn't work. I can't see result. So how can I get amount of posts?

  • Can you please specify what do you mean by not working? Are you getting error? Wrong number? No result at all? – Jack Johansson Apr 19 '17 at 21:01
1

You can use the below to output the post amounts

<?php
$args = array(
  'post_type' => 'post', 
  'showposts' => '10'
);
 $wp_query = new WP_Query($args);
 // amount of posts per on the page
 $post_number = $wp_query->post_count;
 // amount of posts across the post pages
 $post_number2 = $wp_query->found_posts;
 echo $post_number.'<br />';
 echo $post_number2.'<br />';
 while ( have_posts() ) : the_post();
 php get_template_part( 'post', get_post_format() );
 php endwhile; 
?>
1

You could try this $count = $GLOBALS['wp_query']->found_posts This will show total found post on that page global query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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