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?
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?
Share Improve this question asked Jul 26, 2014 at 20:29 Dean ElliottDean Elliott 1492 silver badges7 bronze badges3 Answers
Reset to default 2This 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.";
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;
@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;
}