Using WordPress ‘get_next_post_link()’ PHP function

The get_next_post_link() WordPress PHP function retrieves the next post link that is adjacent to the current post.

Usage

get_next_post_link( $format, $link, $in_same_term, $excluded_terms, $taxonomy );

Parameters

  • $format (string) Optional: Link anchor format. Default ‘« %link’. Default: ‘%link »’.
  • $link (string) Optional: Link permalink format. Default ‘%title’. Default: ‘%title’.
  • $in_same_term (bool) Optional: Whether the link should be in the same taxonomy term. Default: false.
  • $excluded_terms (int|string) Optional: Array or comma-separated list of excluded term IDs. Default: ”.
  • $taxonomy (string) Optional: Taxonomy, if $in_same_term is true. Default ‘category’. Default: ‘category’.

More information

See WordPress Developer Resources: get_next_post_link

Examples

Display Next Post Link

This example displays the next post link, using the default format and link.

echo get_next_post_link();

Custom Anchor Format

This example demonstrates how to use a custom anchor format for the next post link.

echo get_next_post_link( 'Next: %link' );

This example shows how to use a custom permalink format for the next post link.

echo get_next_post_link( '%link »', 'Continue Reading: %title' );

Next Post Link in the Same Category

This example retrieves the next post link in the same category as the current post.

echo get_next_post_link( '%link »', '%title', true );

Exclude Specific Terms

This example retrieves the next post link, excluding posts from specific term IDs.

echo get_next_post_link( '%link »', '%title', false, '5, 10, 15' );