$wp_query->current_post restarts from zero on paged pages

I know how to keep track on the ‘index’ of the current post when running the loop using

$wp_query->current_post

Bu this restarts the count from zero on subsequent paged pages. So if I have 20 posts, and show 5 posts per page, then on page 2 the posts are again numbered 0-4 instead of 5-9.

My question is: Is there a build in way to get the absolute index when running the loop on a paged page, or do I have to calculate the offset myself, using information about page number and posts per page?

2 Answers
2

$wp_query->current_post holds information about current post in loop, not global set of posts. Numering posts across multiple pages is not available natively (as far as I know) and probably would be to unreliable (for example what if number of posts per page changes is customized to be uneven?).

For specific implementation building custom numbering, based on page number is probably indeed only option.

As for stickies you can retrieve their count with something like count( get_option('sticky_posts') ) and adjust your numbering by that.

Leave a Comment