previous_post_link()WP 1.5.0

Displays a link to the previous (by date) post from the specified taxonomy (the default is category).

The function must be used on separate pages (of the is_single() type).

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

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.

To display a link to the following post, see the function: next_post_link().

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

No Hooks.

Return

null. Nothing (null). Display HTML code.

Usage

<?php previous_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 previous post

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

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

#2 Link to the previous post from the category

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

<?php previous_post_link( '%link', 'Previous article from the category', true ); ?>
0

#3 Link to a previous post excluding the category

Display a link to the previous article but not from category 15 (articles from category 15 will not be shown):

<?php previous_post_link( '%link', '%title', false, 15 ); ?>
0

#4 Link to previous post from taxonomy

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

previous_post_link( '%link', '← %title', true, '', 'battle_cat' );

Notes

Changelog

Since 1.5.0 Introduced.

previous_post_link() code WP 6.4.3

function previous_post_link( $format = '&laquo; %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
	echo get_previous_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );
}