next_post_link()WP 1.5.0

Displays a link (tag A) to the next post relative (by date) to the current post from the specified taxonomy (the default is category).

This function should be used inside the loop.

Do not confuse the very similar function next_posts_link(), which displays a link to a page with a list of the following posts, rather than a link to a separate next post.

To display a link to the previous post, use: previous_post_link().

Note for the $in_same_cat parameter. If the post is in several categories at the same time, the selection of next post will be from all these categories. For example, it may turn out that the current post is in categories 1 2 3, and the next in 4 5. Thus, when you click on such links, you will not be able to watch the posts from one category and you will be thrown from one category to another.

It's a wrapper for get_next_post_link().

1 time — 0.006833 sec (very slow) | 50000 times — 16.31 sec (slow) | PHP 7.1.5, WP 4.8.1

No Hooks.

Return

null. Nothing (null). Display HTML code.

Usage

next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
$format(string)
Link format. %link will be replaced with <a> HTML. Example, '%link →' display <a ...> →.
Default: '&laquo; %link'
$link(string)
Link text format. %title will be replaced with post title.
Default: '%title'
$in_same_term(true/false)
Whether link should be in the same taxonomy term. true - get the next post from the current category. If the post in a custom taxonomy, you need to specify the $taxonomy parameter.
Default: false
$excluded_terms(array/string)
Array or comma-separated list of excluded term IDs. You can specify a string or array: '1,5,15' or array(1,5,15).
Default: ''
$taxonomy(string)
The name of the taxonomy for the $in_same_term parameter.
Default: 'category'

Examples

0

#1 Next (Following) link

Show the following link by date of publication and make it bold (HTML tag <strong>):

<?php next_post_link('<strong>%link</strong>'); ?>

Displays: <a rel="next" href="/post_name">Post Title</a>

0

#2 From current category

Output a link to the next article from the current category with the text - "Next article from the category" instead of the title of the article:

<?php next_post_link( '%link', 'Next from current category', true ); ?>
0

#3 Exclude category

Let's show a link to the next article by date of publication, but not from category 13 (articles from category 13 will not be taken into account):

<?php next_post_link('%link', '%title', false, '13'); ?>
0

#4 Taxonomy

A link to the next post from the current term of the specified taxonomy. We need a taxonomy called battle_cat:

next_post_link( '%link', '← %title', true, '', 'battle_cat' );
0

#5 A looped output

If you need to loop the output, i.e. if there is no next entry, then output the first post and Vice versa, use this code:

/**
 * Looped output of the previous and next post in WordPress
 */
if( get_adjacent_post(false, '', true) ) { 
	previous_post_link('%link', '← Previous Post');
}
else { 
	$first = new WP_Query('posts_per_page=1&order=DESC');
	$first->the_post();

	echo '<a href="' . get_permalink() . '">← Previous Post</a>';

	wp_reset_postdata();
}; 

if( get_adjacent_post(false, '', false) ) { 
	next_post_link('%link', 'Next Post →');
}
else { 
	$last = new WP_Query('posts_per_page=1&order=ASC');
	$last->the_post();

	echo '<a href="' . get_permalink() . '">Next Post →</a>';

	wp_reset_postdata();
}; 

Notes

Changelog

Since 1.5.0 Introduced.

next_post_link() code WP 6.4.3

function next_post_link( $format = '%link &raquo;', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
	echo get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
}