Using WordPress ‘next_post()’ PHP function

The next_post() WordPress PHP function prints a link to the next post.

Usage

next_post($format, $next, $title, $in_same_cat, $limitnext, $excluded_categories);

Parameters

  • $format (string) Optional. Default: ‘%’. Format for the link.
  • $next (string) Optional. Default: ‘next post: ‘. Text for the next post link.
  • $title (string) Optional. Default: ‘yes’. Display the title of the next post.
  • $in_same_cat (string) Optional. Default: ‘no’. Show the next post only if it’s in the same category.
  • $limitnext (int) Optional. Default: 1. Limit the number of next posts displayed.
  • $excluded_categories (string) Optional. Default: ”. Exclude specific categories by ID.

More information

See WordPress Developer Resources: next_post()

Examples

Basic usage of next_post()

Print the link to the next post with default settings.

next_post();

Display the next post link with custom text

Customize the text for the next post link.

next_post('%', 'Read the next article: ');

Display the next post link without post title

Hide the title of the next post in the link.

next_post('%', 'Next: ', 'no');

Display the next post link in the same category

Show the next post link only if it’s in the same category as the current post.

next_post('%', 'Next in category: ', 'yes', 'yes');

Exclude specific categories

Exclude next post links from specific categories by ID (e.g., category IDs 5 and 9).

next_post('%', 'Next post: ', 'yes', 'no', 1, '5,9');