Using WordPress ‘get_the_post_navigation()’ PHP function

The get_the_post_navigation() WordPress PHP function retrieves the navigation to the next/previous post when applicable.

Usage

echo get_the_post_navigation(array(
  'prev_text' => 'Previous post: %title',
  'next_text' => 'Next post: %title',
));

Parameters

  • $args (array) – Optional. Default post navigation arguments:
    • prev_text (string) – Anchor text to display in the previous post link. Default ‘%title’.
    • next_text (string) – Anchor text to display in the next post link. Default ‘%title’.
    • in_same_term (bool) – Whether the link should be in the same taxonomy term. Default false.
    • excluded_terms (int | string) – Array or comma-separated list of excluded term IDs.
    • taxonomy (string) – Taxonomy, if $in_same_term is true. Default ‘category’.
    • screen_reader_text (string) – Screen reader text for the nav element. Default ‘Post navigation’.
    • aria_label (string) – ARIA label text for the nav element. Default ‘Posts’.
    • class (string) – Custom class for the nav element. Default ‘post-navigation’.

More information

See WordPress Developer Resources: get_the_post_navigation

Examples

Basic usage

Display the default post navigation:

echo get_the_post_navigation();

Custom previous and next text

Customize the anchor text for previous and next post links:

echo get_the_post_navigation(array(
  'prev_text' => 'Previous: %title',
  'next_text' => 'Next: %title',
));

Display post navigation only within the same category:

echo get_the_post_navigation(array(
  'in_same_term' => true,
));

Exclude specific terms

Exclude posts from specific categories or terms:

echo get_the_post_navigation(array(
  'excluded_terms' => '5,10,15',
));

Custom taxonomy navigation

Display post navigation within a custom taxonomy:

echo get_the_post_navigation(array(
  'in_same_term' => true,
  'taxonomy' => 'custom_taxonomy',
));