1

I'd like to be able to display the number of posts per page and the total number of posts. This question (Show number of posts AND number on current page) was previously asked and a solution provided by karpstrucking:

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.";

This works really well (thanks karpstrucking), but there is an issue with the last page; if, for example, your pages are set to show 10 posts per page, and there are 23 posts in total, the pages will show:

  • Showing posts 1 - 10 of 23
  • Showing posts 11 - 20 of 23
  • Showing posts 21 - 30 of 23

Some sort of conditional required, but unfortunately I really don't know where to start.

Any help hugely appreciated.

1 Answer 1

2

Just change the way you calculate $start and $end:

$page  = max( 1, get_query_var( 'paged' ) );
$ppp   = get_query_var('posts_per_page');
$start = $ppp * ( $page - 1 ) + 1;
$end   = $start + $wp_query->post_count - 1; // Start post plus number of posts for current page (not *total* posts for the query)
2
  • Many thanks TheDeadMedic for your help!! This is very close, but not quite; I have plus 1 for the end number of all pages ('1 - 11 of 23', '11 - 21 of 23' & '21-24 of 23').
    – scribbles
    Commented Sep 7, 2015 at 9:30
  • 1
    Sorry, should have worked a bit harder before commenting: I have it now: $end = $start + $wp_query->post_count - 1; Thanks a million for your help!!
    – scribbles
    Commented Sep 7, 2015 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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