get_previous_post_link()
Gets a link (tag A) to the previous post relative (by date) to the current post.
The function should be used on separate pages of is_single() type.
To get a link to the next post, use get_next_post_link().
Uses: get_adjacent_post_link()
Used By: previous_post_link()
1 time — 0.005912 sec (very slow) | 50000 times — 16.36 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_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: '« %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
#1 Output a link to the previous post
Here I will give examples of how to use this function in different ways:
# link to previous post echo get_previous_post_link(); # display: ← <a href="http://test.ru/zagolovok" rel="prev">Title of post</a> # link to the previous entry from the current category echo get_previous_post_link( '%link', '← %title', 1 ); # display: <a href="http://test.ru/markup" rel="prev">← Markup</a> # link to the previous post from the current taxonomy element (my_tax), for custom taxonomies echo get_previous_post_link( '%link', '%title →', 1, '', 'my_tax' ); # link to the previous post without taking into account posts from categories 5 and 10 echo get_previous_post_link( '%link', '%title →', 0, '5,10' );
Changelog
Since 3.7.0 | Introduced. |
get_previous_post_link() get previous post link code WP 6.7.2
function get_previous_post_link( $format = '« %link', $link = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) { return get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, true, $taxonomy ); }