get_adjacent_post_rel_link() WordPress Function

The get_adjacent_post_rel_link() function is used to get the previous or next post, in relation to the current post. This function is used when a plugin needs to display a link to the previous or next post.

get_adjacent_post_rel_link( string $title = '%title', bool $in_same_term = false, int[]|string $excluded_terms = '', bool $previous = true, string $taxonomy = 'category' ) #

Retrieves the adjacent post relational link.


Description

Can either be next or previous post relational link.


Top ↑

Parameters

$title

(string)(Optional) Link title format.

Default value: '%title'

$in_same_term

(bool)(Optional) Whether link should be in a same taxonomy term.

Default value: false

$excluded_terms

(int[]|string)(Optional) Array or comma-separated list of excluded term IDs.

Default value: ''

$previous

(bool)(Optional) Whether to display link to previous or next post.

Default value: true

$taxonomy

(string)(Optional) Taxonomy, if $in_same_term is true.

Default value: 'category'


Top ↑

Return

(string|void) The adjacent post relational link URL.


Top ↑

Source

File: wp-includes/link-template.php

function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
	$post = get_post();
	if ( $previous && is_attachment() && $post ) {
		$post = get_post( $post->post_parent );
	} else {
		$post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
	}

	if ( empty( $post ) ) {
		return;
	}

	$post_title = the_title_attribute(
		array(
			'echo' => false,
			'post' => $post,
		)
	);

	if ( empty( $post_title ) ) {
		$post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
	}

	$date = mysql2date( get_option( 'date_format' ), $post->post_date );

	$title = str_replace( '%title', $post_title, $title );
	$title = str_replace( '%date', $date, $title );

	$link  = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
	$link .= esc_attr( $title );
	$link .= "' href='" . get_permalink( $post ) . "' />\n";

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

	/**
	 * Filters the adjacent post relational link.
	 *
	 * The dynamic portion of the hook name, `$adjacent`, refers to the type
	 * of adjacency, 'next' or 'previous'.
	 *
	 * Possible hook names include:
	 *
	 *  - `next_post_rel_link`
	 *  - `previous_post_rel_link`
	 *
	 * @since 2.8.0
	 *
	 * @param string $link The relational link.
	 */
	return apply_filters( "{$adjacent}_post_rel_link", $link );
}


Top ↑

Changelog

Changelog
VersionDescription
2.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More