get_next_post_link()WP 3.7.0

Gets a link (tag A) to the next post relative (by date) to the current post.

To get a link to the previous post, use get_previous_post_link().

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

No Hooks.

Return

String.

  • HTML of link:

    <a rel="next" href="/some-post-name">Post title</a>
  • An empty string if there is no suitable post.

Usage

get_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 Link to the next post relative to the current one

echo get_next_post_link();
# display: <a href="http://example.com/some" rel="next">Что-то</a> →      

# link to the next post from the current category
echo get_next_post_link( '%link', '%title →', 1 );
# display: <a href="http://example.com/some" rel="next">Что-то →</a>

# link to the next post from the current element in the taxonomy my_tax
echo get_next_post_link( '%link', '%title →', 1, '', 'my_tax' );
0

#2 Add own class to links

When you need to add an arbitrary class to the next/previous link, you can use this hack:

$next = get_next_post_link( '%link', 'next post', true ); 
echo str_replace( '<a ', '<a class="myclass" ', $next );

$prev = get_previous_post_link( '%link', 'prev post', true );
echo str_replace( '<a ', '<a class="myclass" ', $prev );

Changelog

Since 3.7.0 Introduced.

get_next_post_link() code WP 6.4.3

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