Functions such as get_next_posts_link allow you to get the HTML link to the next page, with few parameters for change (label and max pages). I want to get the URL to the next page so I can add custom classes to the link.
Why build the link yourself, when you can just add the class to the result of
Now the link sent back by
While a lot of common functions can be found on the WordPress Codex, there are also still undocumented functions in the source code.
The previous_posts function is one such function, which is documented in the source:
Using previous_posts(false), we can get the previous page's URL, without displaying.
However this doesn't check for when there are no more pages, and will return 404s if used on it's own.
Inspecting the source of get_next_posts_link, we can use the code for checking if we have reached the page boundary, and return just the link: