WordPress Get next and previous post URL permalink

Hello,

This post as the title says talks about getting URL for next and previous post. Please note that post is not same as page.

All the required functions and hints can be found in link-template.php file under wp-includes

Let me show you how you can get permalinks for next and previous post without doing anything fancy.

Get Previous post URL

First of all we will get hold of post object for the previous post as shown below

We will use adjacent_post_link() function. Definition of this function is given below

So to get the previous post link do this

Above usage of function get_adjacent_post pass on default values for all the params function accepts. You can pass on custom values for it to behave differently.

Now lets check how we can get next post permalink

Get Next post URL

Pretty much the same functionality but with one core difference. $previous should be set to false as shown below

 

 

As you can see that its pretty straight forward.

I hope this helps. Please let me know if you have something to add of the above code can be improved. Don’t forget to leave you comments. Once again I would like to add that getting post link is not same as getting page link.

Here is a useful read from wordpress codex

http://codex.wordpress.org/Next_and_Previous_Links

The above post explains how to get page URLs.

If you have any questions please let me know so that either myself or any other reader reading this post can answer your question.s

Cheers

 

 

 

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.