I’m wondering if there is a good way to tell which archive page a post came from. I essentially just need the post’s position in the total order, then divide it by the ‘posts_per_page’ option. The hangup I’m having is getting that position or offset of where the post sits.
EDIT: All while being on the SINGLE POST template.
There, no matter what, the usual $wp_query global and ‘page’/’paged’ query vars are always 0 – so those won’t get me anywhere.
Alright, nevermind. I got this solved by doing this at the top of the single post template:
If you’re on page 2+, you’ll have access to the
global $page
, which should tell you the pagenumber you’re currently on. There’re also some others that are worth a look (never know where which one is present/active):global $multipage, $numpages;
.You can also check the current page via the
get_query_var();
core function. Basically theglobal $page
comes from there.I needed something like this for singles only and slightly simplified the accepted answer:
This can used in this context: ‘Page 1 of 10’ 1 being $current_page and 10 being $total_posts
When faced with this same requirement I opted to use a direct MySQL call to calculate which page the current post is on without retrieving and looping every post in this post_type. I went this route because I discovered it is much faster when working on a site with thousands of posts.
This approach may also be put inside a function/method and determined from anywhere.
If you find you are calling it often (like within a template) you can store the result in the object cache if desired.
We have 2 Conditional Tags to make this simpler
Paste This in the File which includes in both page and post and changed from “post” & “Page” to whatever your need is.