0

I know how to show the total number of posts in a site, but is there a way to show the number of posts the page you're on is displaying?

For example, I want to show this at the bottom of each of my blog pages (8 posts per page);

Index - "Showing posts 1 through 8 of 294 total"
Page2 - "Showing posts 9 through 17 of 294 total"
Page3 - "Showing posts 18 through 26 of 294 total"

The posts are displayed in chronological order if that makes a difference.

Is this even possible?

1

This should work, but I haven't tested it:

global $wp_query;
$page = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$ppp = get_query_var('posts_per_page');
$end = $ppp * $page;
$start = $end - $ppp + 1;
$total = $wp_query->found_posts;
echo "Showing posts $start through $end of $total total.";
1
  • 2
    get_query_var('posts_per_page') will overshoot on the last page of the results (sometimes the first). Has to use $wp_query->post_count.
    – soulseekah
    Aug 30 '17 at 18:00
0

Because I'm doing that right now, I tested the last solution and it works except on the last page.

That's working now and tested:

global $wp_query;
$page = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$ppp = get_query_var('posts_per_page');
$end = $ppp * $page;
$start = $end - $ppp + 1;
$total = $wp_query->found_posts;
if ($end > $total)
  $end = $total;
0

@karpstrucking is really close but if the number of found posts is smaller than the number posts allowed per page, you'll echo a message like "Showing posts 1 through 12 of 9 total". To prevent this, the code should be updated to something like:

    global $wp_query;
    $page = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
    $ppp = get_query_var('posts_per_page');
    $total = $wp_query->found_posts;
    $end = $ppp * $page;
    $start = $end - $ppp + 1;

    if( $ppp > $total ) {
        $results_summary_html = '<p>Showing ' . $total . ' out of ' . $total;
    } else if( $end > $total ) {
        $results_summary_html = '<p>Showing ' . $start . '-' . $total . ' out of ' . $total;
    } else {
        $results_summary_html = '<p>Showing ' . $start . '-' . $end . ' out of ' . $total;
    }

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.