get_adjacent_post_link()
Gets the adjacent (next/previous) post link (HTML tag A) from the specified taxonomy (the default is category).
Uses on pages of type is_single().
There are wrappers for this function:
Uses: get_adjacent_post()
Used By: get_previous_post_link(), get_next_post_link()
Hooks from the function
Returns
String. The HTML link of the previous or next post in relation to the current post.
<a rel="nofollow" href="/post_name">Post title</a>
Usage
get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
- $format(string) (required)
- Link anchor format.
%linkwill be replaced with link HTML code <a>. Example,'%link →'display<a ...> →. - $link(string) (required)
- Link format.
'%title'will be replaced by the title of the post. - $in_same_term(true/false)
- true - will search for related posts in the same taxonomy element (category) in which the current post is. false - will look for links of all taxonomy elements.
Default: false - $excluded_terms(array/string)
- Array or comma-separated list of excluded terms IDs (terms from which we don't need posts). Example:
'1,5,15'orarray(1,5,15).
Default: '' - $previous(true/false)
- Whether to display link to previous or next post.
Default: true - $taxonomy(string)
- Taxonomy name, if $in_same_term is true.
Default: 'category'
Examples
#1 Demonstration
Below are the options for calling the function and the results:
# link to previous post echo get_adjacent_post_link( '← %link', '%title' ); # display: ← <a href="http://test.ru/markup" rel="prev">Marking</a> # link to next post echo get_adjacent_post_link( '%link →', '%title', 0, '', false ); # display: <a href="http://example.com/some" rel="next">Some</a> → # link to the previous post from the current category echo get_adjacent_post_link( '%link', '← %title', 1 ); # display: <a href="http://test.ru/markup" rel="prev">← Marking</a> # link to the next post from the current category echo get_adjacent_post_link( '%link', '%title →', 1, '', false ); # display: <a href="http://example.com/some" rel="next">Some →</a> # link to the previous post from the current taxonomy element (my_tax), for custom taxonomies echo get_adjacent_post_link( '%link', '%title →', 1, '', 1, 'my_tax' ); # link to the next post from the current taxonomy element (my_tax) echo get_adjacent_post_link( '%link', '%title →', 1, '', 0, 'my_tax' ); # link to the previous post without taking into account the categories 5 and 10 echo get_adjacent_post_link( '%link', '%title →', 0, '5,10' );
#2 Сircular linking function
The feature of the function is that if there are no next or prev posts, the link to the first or last post will be displayed accordingly.
/**
* Displays a link (HTML tag A) to adjacent records (next/previous).
*
* If there is no adjacent post, displays the posts from the opposite side. Works within the category where the post itself.
*
* @param string $course take the values next/prev.
*/
function da_the_adjacent_post_link( $course = '' ){
global $post;
$course = ( $course == 'prev' ) ? true : false;
$order = ( $course ) ? 'DESC' : 'ASC';
$class = ( $course ) ? 'prev' : 'next';
$link = get_adjacent_post_link( '%link', '%title', true, '', $course );
if ( ! $link ){
$term = get_the_terms( $post->ID, 'category' );
$term = $term[0];
$article = get_posts([
'numberposts' => 1,
'exclude' => $post->ID,
'category' => $term->term_id,
'order' => $order
]);
if ( empty($article) )
return false;
else
$article = $article[0];
$link = sprintf( '<a href="%s" rel="%s">%s</a>', get_the_permalink($article->ID), $class, $article->post_title );
}
echo $link;
}
Changelog
| Since 3.7.0 | Introduced. |