Skip to main content
deleted 2 characters in body
Source Link
Krzysiek Dróżdż
  • 25.5k
  • 9
  • 53
  • 74

I'm afraid you're doing it a little bit wrong...

Here are the problems:

  1. post_count field contains the number of posts being displayed and not the offset.
  2. wp_count_posts returns the number of posts globally, not in current query (so it will not be true for queries that have some filters).
  3. echo $post_count; . '-' . $post_count; is not correct PHP code.

So how to do this?

All you need is the info from WP_Query:

<?php
    $first_post = absint( ( $wp_query->get('paged') - 1 ) * $wp_query->get('posts_per_page') + 1;
    $last_post = $first_post + $wp_query->post_count;
    $all_posts = $wp_query->found_posts;
?>
<p class="small text-uppercase">Showing <?php echo $first_post . '-' . $last_post; ?> of <strong><?php echo $all_posts; ?> items</strong></p>

I'm afraid you're doing it a little bit wrong...

Here are the problems:

  1. post_count field contains the number of posts being displayed and not the offset.
  2. wp_count_posts returns the number of posts globally, not in current query (so it will not be true for queries that have some filters).
  3. echo $post_count; . '-' . $post_count; is not correct PHP code.

So how to do this?

All you need is the info from WP_Query:

<?php
    $first_post = absint( ( $wp_query->get('paged') - 1 ) * $wp_query->get('posts_per_page') + 1;
    $last_post = $first_post + $wp_query->post_count;
    $all_posts = $wp_query->found_posts;
?>
<p class="small text-uppercase">Showing <?php echo $first_post . '-' . $last_post; ?> of <strong><?php echo $all_posts; ?> items</strong></p>

I'm afraid you're doing it a little bit wrong...

Here are the problems:

  1. post_count field contains the number of posts being displayed and not the offset.
  2. wp_count_posts returns the number of posts globally, not in current query (so it will not be true for queries that have some filters).
  3. echo $post_count; . '-' . $post_count; is not correct PHP code.

So how to do this?

All you need is the info from WP_Query:

<?php
    $first_post = absint( $wp_query->get('paged') - 1 ) * $wp_query->get('posts_per_page') + 1;
    $last_post = $first_post + $wp_query->post_count;
    $all_posts = $wp_query->found_posts;
?>
<p class="small text-uppercase">Showing <?php echo $first_post . '-' . $last_post; ?> of <strong><?php echo $all_posts; ?> items</strong></p>
added 1 character in body
Source Link
Krzysiek Dróżdż
  • 25.5k
  • 9
  • 53
  • 74

I'm afraid you're doing it a little bit wrong...

Here are the problems:

  1. post_count field contains the number of posts being displayed and not the offset.
  2. wp_count_posts returns the number of posts globally, not in current query (so it will not be true for queries that have some filters).
  3. echo $post_count; . '-' . $post_count; is not correct PHP code.

So how to do this?

All you need is the info from WP_Query:

<?php
    $first_post = absint( ( $wp_query->get('page''paged') - 1 ) * $wp_query->get('posts_per_page') + 1;
    $last_post = $first_post + $wp_query->post_count;
    $all_posts = $wp_query->found_posts;
?>
<p class="small text-uppercase">Showing <?php echo $first_post . '-' . $last_post; ?> of <strong><?php echo $all_posts; ?> items</strong></p>

I'm afraid you're doing it a little bit wrong...

Here are the problems:

  1. post_count field contains the number of posts being displayed and not the offset.
  2. wp_count_posts returns the number of posts globally, not in current query (so it will not be true for queries that have some filters).
  3. echo $post_count; . '-' . $post_count; is not correct PHP code.

So how to do this?

All you need is the info from WP_Query:

<?php
    $first_post = absint( ( $wp_query->get('page') - 1 ) * $wp_query->get('posts_per_page') + 1;
    $last_post = $first_post + $wp_query->post_count;
    $all_posts = $wp_query->found_posts;
?>
<p class="small text-uppercase">Showing <?php echo $first_post . '-' . $last_post; ?> of <strong><?php echo $all_posts; ?> items</strong></p>

I'm afraid you're doing it a little bit wrong...

Here are the problems:

  1. post_count field contains the number of posts being displayed and not the offset.
  2. wp_count_posts returns the number of posts globally, not in current query (so it will not be true for queries that have some filters).
  3. echo $post_count; . '-' . $post_count; is not correct PHP code.

So how to do this?

All you need is the info from WP_Query:

<?php
    $first_post = absint( ( $wp_query->get('paged') - 1 ) * $wp_query->get('posts_per_page') + 1;
    $last_post = $first_post + $wp_query->post_count;
    $all_posts = $wp_query->found_posts;
?>
<p class="small text-uppercase">Showing <?php echo $first_post . '-' . $last_post; ?> of <strong><?php echo $all_posts; ?> items</strong></p>
Source Link
Krzysiek Dróżdż
  • 25.5k
  • 9
  • 53
  • 74

I'm afraid you're doing it a little bit wrong...

Here are the problems:

  1. post_count field contains the number of posts being displayed and not the offset.
  2. wp_count_posts returns the number of posts globally, not in current query (so it will not be true for queries that have some filters).
  3. echo $post_count; . '-' . $post_count; is not correct PHP code.

So how to do this?

All you need is the info from WP_Query:

<?php
    $first_post = absint( ( $wp_query->get('page') - 1 ) * $wp_query->get('posts_per_page') + 1;
    $last_post = $first_post + $wp_query->post_count;
    $all_posts = $wp_query->found_posts;
?>
<p class="small text-uppercase">Showing <?php echo $first_post . '-' . $last_post; ?> of <strong><?php echo $all_posts; ?> items</strong></p>