Using WordPress ‘previous_post()’ PHP function

The previous_post() WordPress PHP function displays a link to the previous post.

Usage

previous_post($format = '%', $previous = 'previous post: ', $title = 'yes', $in_same_cat = 'no', $limitprev = 1, $excluded_categories = '');

Parameters

  • $format (string) Optional. Default: ‘%’. Determines the format of the link.
  • $previous (string) Optional. Default: ‘previous post: ‘. The text to display before the post title.
  • $title (string) Optional. Default: ‘yes’. Whether to show the post title in the link.
  • $in_same_cat (string) Optional. Default: ‘no’. Whether to show only previous posts from the same category.
  • $limitprev (int) Optional. Default: 1. Limit the number of previous posts to display.
  • $excluded_categories (string) Optional. Default: ”. A comma-separated list of category IDs to exclude.

More information

See WordPress Developer Resources: previous_post

Examples

Display a simple previous post link

previous_post();
// Output: "<a href="previous-post-url">previous post: Previous Post Title</a>"

Display previous post link with custom format and text

previous_post('&laquo; %', 'Check out: ', 'yes');
// Output: "<a href="previous-post-url">Check out: Previous Post Title</a>"

Display previous post link without post title

previous_post('&laquo; %', 'Go back to the previous post: ', 'no');
// Output: "<a href="previous-post-url">Go back to the previous post:</a>"

Display previous post link within the same category

previous_post('&laquo; %', 'Previous post in the same category: ', 'yes', 'yes');
// Output: "<a href="previous-post-url">Previous post in the same category: Previous Post Title</a>"

Display previous post link excluding certain categories

previous_post('&laquo; %', 'Previous post: ', 'yes', 'no', 1, '3,5');
// Output: "<a href="previous-post-url">Previous post: Previous Post Title</a>"