Next and Previous Links

The Next and Previous post links guides your visitor through your WordPress site. When it comes to creating strong site-wide navigation, some of the most powerful tools for moving your visitor around are these link tags.

There are two sets of tags that move the visitor through your WordPress site: posts_nav_link(), which displays both the Previous and Next links, and the combination pair of previous_post() and next_post(), which each display one of the Previous or Next links. This article will look at how these two tag sets work.

Note: "Previous" and "Next" in this case refer to posts in the order that they are in, not to any particular direction in time. This often confuses many people, as WordPress, by default displays posts starting from the newest and proceeding backwards in time. Using this default ordering, "Next" would be moving backwards in time, because the "Next" page after page 1 would be page 2, and that would move to older posts. If the post ordering is changed (like via a manual usage of query_posts in a template), then the links will point in different directions. This codex article uses both methods without explanation, because it is example code only. So it is important to keep in mind that the function is referring to an order that is independent of chronological time.

Contents

The posts_nav_link

The first set of these site navigation links is featured only on the non-single/non-permalink web pages, such as categories, archives, searches, and the index page. It is the template tag posts_nav_link(). This tag creates two links at the bottom of the page within the WordPress Loop to display the next and previous pages in chronological order.

We have just uncovered the surface, but you can let your imagination and web page design skills create these any way you like, adding borders, background images, stylized text, and more.

The Next and Previous Posts

The other set of navigational aids for moving through your site control the next post and previous post links typically found at the bottom of your single/permalink post, such as any single post or article you have published on your site. These direct the user to move to the next or previous post in chronological order.

Text used in combination with the '%' to represent the permalink to the post. The default is the permalink.

text

Text displayed before the permalink. The default is "next post" and "previous post".

title

This turns "on" and "off" the use of the post title to be used as the link text. By default, is it "yes". If set to "no", then only the text set in the text parameter and format would show.

Let's put these into action.

The following example displays the next and previous post titles with arrows to emphasize the direction the user may choose. You will notice that we have not set the text parameter, so it will be blank.

A useful plugin called "Better Adjacent Post Links" allows you to trim the title of the previous and next posts to any length you see fit. This is useful when you have longer titles that break the site's design.

This is just an introduction on how to use these tags and do fun things with them, but you can do so much more by adding borders, background images, interesting fonts and colors - it's up to you! Have fun!