Using WordPress ‘get_adjacent_post_rel_link()’ PHP function

The get_adjacent_post_rel_link() WordPress PHP function retrieves the adjacent post relational link, which can be either the next or previous post link.

Usage

get_adjacent_post_rel_link( $title, $in_same_term, $excluded_terms, $previous, $taxonomy );

Parameters

  • $title (string) – Optional. The link title format. Default is ‘%title’.
  • $in_same_term (bool) – Optional. Whether the link should be in the same taxonomy term. Default is false.
  • $excluded_terms (int | string) – Optional. Array or comma-separated list of excluded term IDs. Default is ”.
  • $previous (bool) – Optional. Whether to display the link to the previous or next post. Default is true.
  • $taxonomy (string) – Optional. Taxonomy, if $in_same_term is true. Default is ‘category’.

More information

See WordPress Developer Resources: get_adjacent_post_rel_link()

Examples

Display next post link

This example displays the next post link in the same category.

echo get_adjacent_post_rel_link( '%title', true, '', false );

Display previous post link

This example displays the previous post link in the same category.

echo get_adjacent_post_rel_link( '%title', true );

Exclude specific terms

This example displays the next post link, excluding posts in terms 5 and 15.

echo get_adjacent_post_rel_link( '%title', false, '5,15', false );

Display next post link in custom taxonomy

This example displays the next post link in the same custom taxonomy called ‘genre’.

echo get_adjacent_post_rel_link( '%title', true, '', false, 'genre' );

This example displays the next post link with a custom link title format.

echo get_adjacent_post_rel_link( 'Next: %title', true, '', false );