Get the Last Page of a Post in WordPress

Here’s a quick function to determine whether or not the viewer is on the last page of a paginated post. It’s useful if you want to display information (comments, post meta, etc.) only on the last page.

While it’s possible to paginate long posts in WordPress using the quicktag <!--nextpage--> and the template tag wp_link_pages(), I needed a way to determine when the viewer was on the last page of the post.

The reasoning behind this was because I wanted comments and post meta information to be displayed only on the last page of the post. The default pagination of long posts results in this information being displayed at the bottom of every page of your post. Imagine a post with over 100+ comments, loading every time a viewer clicked on a page link. Not to mention aesthetics; showing redundant information over and over.

I looked through the WP Codex for information regarding pagination but didn’t find what I was looking for. A search in the majors (Google, Yahoo, MSN) returned few results as well. I thought I was going to have a write a specific database query to get what I was looking for, but scanning through the WP core (which I always review, but never mess with), I found a deceptively easy solution.

Continues…

Pages: 1 2