On many WordPress themes you see links at the bottom of single posts which direct you to the previous and next posts of the blog. So, in this short tutorial, I am going to show you how to use the built-in WordPress functions to create those links.
There is a function for both the previous link and the next link, both of these functions should be used on a single post within the loop. Both the functions will echo the links out on the screen and will allow you to change the format of the links on the page.
The Previous Post Link
WordPress has the function
previous_post_link which should be used in the loop of the single posts. This function will create a link to the previous post by date. If there are no posts then a link will not be displayed.
To use the previous post link just use the below snippet:
<?php previous_post_link($format, $link, $in_same_cat = false, $excluded_categories = ''); ?>
This function accepts 4 parameters:
$format– This will allow you to change the format of the outputted link. To define just the link, use the token
$link– This defines the text that is used on the link. The default is
%titleof the post.
$in_same_cat– This is a boolean which defines if the linked post is in the same category or not. The default is false.
$excluded_categories– This allows you to define which categories are excluded from being linked to.
Examples Of Using Previous Post Link
All of the parameters in the
previous_post_link are optional, so the simplest use of this function is to use no parameters:
<?php previous_post_link(); ?>
Change Link Format
To change the format of the link you can wrap it in a
h1 tag by using the first parameter:
<?php previous_post_link('<h1>%link</h1>'); ?>
Change Link Text
To change the text of the link you need to change the second parameter:
<?php previous_post_link('%link', 'Link To Previous Post'); ?>
Link To Posts In Same Category
To only link to previous posts in the same category use the following snippet:
<?php previous_post_link('« %link', '%title', TRUE); ?>
Exclude Links in Category
To exclude any categories from being linked to from this link you need to use the 4th parameter. To exclude all posts with a category ID of 17 use the following snippet:
<?php previous_post_link('« %link', '%title', FALSE, '17'); ?>
Next Post Link
next_post_link function works exactly the same way as the previous link but will return the next post. This takes exactly the same parameters as the previous post link function and will work the exact same way.
If there is no next post then the link will not be displayed.
<?php next_post_link('format', 'link', 'in_same_cat', 'excluded_categories'); ?>
Use In A Theme
When you use these in your WordPress theme you would want the previous link to float to the left and the next post link to float to the right.
Here is a real world example of using these functions.
<?php previous_post_link('<span class="left">« %link</span>'); next_post_link('<span class="right">%link »</span>'); ?> <div class="clearfix"></div>
This adds a span around both of the link adding a CSS class of left or right so you can float the link. We then use the clearfix example to clear the floats on the page.
« WordPress Database Schema and Tables You Should Know
Filtering post or custom post type by a specific taxonomy term in the WordPress admin »