Using WordPress ‘adjacent_posts_rel_link()’ PHP function

The adjacent_posts_rel_link() WordPress PHP function displays the relational links for the posts adjacent to the current post.

Usage

adjacent_posts_rel_link($title = '%title', $in_same_term = false, $excluded_terms = '', $taxonomy = 'category');

This will create relational links for the posts adjacent to the current post. It will use the post title as the link title format, not consider taxonomy terms, not exclude any terms, and the default taxonomy is ‘category’.

Parameters

  • $title (string): Optional. Link title format. Default is ‘%title’.
  • $in_same_term (bool): Optional. Whether 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 ”.
  • $taxonomy (string): Optional. Taxonomy, if $in_same_term is true. Default is ‘category’.

More information

See WordPress Developer Resources: adjacent_posts_rel_link()

Examples

Example 1

Display links using default parameters:

adjacent_posts_rel_link();

This will display links to the posts adjacent to the current post, using the post title as the link title.

Example 2

Display links in the same taxonomy term:

adjacent_posts_rel_link('%title', true);

This will display links to posts in the same taxonomy term as the current post.

Example 3

Exclude specific terms:

adjacent_posts_rel_link('%title', false, '5,10,15');

This will display links to the posts adjacent to the current post, but exclude the terms with IDs 5, 10, and 15.

Example 4

Specify a custom taxonomy:

adjacent_posts_rel_link('%title', true, '', 'my_taxonomy');

This will display links to the posts in the same custom taxonomy (‘my_taxonomy’) as the current post.

Example 5

Use a different link title format:

adjacent_posts_rel_link('Next Post: %title');

This will display links to the posts adjacent to the current post, but the link title will be “Next Post: [Post Title]”.