WordPress at a glance

get_adjacent_post_link() WP 3.7.0

Gets the adjacent (next/previous) post link (HTML tag A). You can specify limitations on the elements of taxonomies (categories, tags). Can be either next post link or previous.

Uses on pages of type is_single().

There are wrappers for this function: previous_post_link() and next_post_link().

Works based on: get_adjacent_post()
Hooks from function:
Return

String. The HTML link of the previous or next post in relation to the current post.

<a rel="nofollow" href="/post_name">Заголовок поста</a>

Usage

get_adjacent_post_link( $format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy );
$format(string) (required)
Link anchor format. %link will 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' or array(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://wptest.ru/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://wptest.ru/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;
}

Code of get_adjacent_post_link: wp-includes/link-template.php VER 4.9.8

<?php
function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
	if ( $previous && is_attachment() )
		$post = get_post( get_post()->post_parent );
	else
		$post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );

	if ( ! $post ) {
		$output = '';
	} else {
		$title = $post->post_title;

		if ( empty( $post->post_title ) )
			$title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );

		/** This filter is documented in wp-includes/post-template.php */
		$title = apply_filters( 'the_title', $title, $post->ID );

		$date = mysql2date( get_option( 'date_format' ), $post->post_date );
		$rel = $previous ? 'prev' : 'next';

		$string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">';
		$inlink = str_replace( '%title', $title, $link );
		$inlink = str_replace( '%date', $date, $inlink );
		$inlink = $string . $inlink . '</a>';

		$output = str_replace( '%link', $inlink, $format );
	}

	$adjacent = $previous ? 'previous' : 'next';

	/**
	 * Filters the adjacent post link.
	 *
	 * The dynamic portion of the hook name, `$adjacent`, refers to the type
	 * of adjacency, 'next' or 'previous'.
	 *
	 * @since 2.6.0
	 * @since 4.2.0 Added the `$adjacent` parameter.
	 *
	 * @param string  $output   The adjacent post link.
	 * @param string  $format   Link anchor format.
	 * @param string  $link     Link permalink format.
	 * @param WP_Post $post     The adjacent post.
	 * @param string  $adjacent Whether the post is previous or next.
	 */
	return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent );
}

Related Functions

From tag: adjacent links (related links)

More from category: Navigation

No comments
    Hello, !     Log In . Register