WordPress makes it really easy to link to next and previous posts, but not easy to work with neighboring pages.
WordPress Next Page ID is a set of two simple functions that get the ID of the previous and next pages (or subpages).
The Use Case #
I recently built a scavenger hunt app using WordPress, and needed to get info about the pages before and after each current challenge. Since each clue builds on the one before, I needed to automate the process of validating that each challenge was completed and get a link to the next one.
Matt McInvale built Next Page, Not Next Post, an plugin for linking to neighboring pages.
Matt’s plugin was a bit more robust than what I needed, but I also needed more than just links. I need to get the IDs of neighboring pages so I could query metadata and dynamically serve content.
By breaking out Matt’s plugin into two simple functions, I now have lightweight, DRY code that I’m sure I’ll reuse on future projects.
How It Works #
WordPress Next Page ID is just two simple functions:
Include the two functions in the PHP file you’re working in. Then call each function as needed. Replace
$id with the ID of the page you’re trying to get the next or previous page ID for.
$next_ID = next_page_ID('41); $previous_ID = previous_page_ID('41); $next_url = get_permalink($next_ID); echo '<a href="' . $next_url . '">Next Page</a>';