Creating Custom Post Navigation in WordPress Themes

Posted on 19th June 2023

In this article we will be discussing how to create custom post navigation in WordPress themes. We will be covering the following topics:

  • What is post navigation?
  • Why is post navigation important?
  • How to create custom post navigation in WordPress themes?

What is post navigation?

Post navigation is the process of moving from one post to another in a WordPress site. This can be done either by clicking on the next/previous post links which are usually located at the bottom of each post, or by using the keyboard shortcuts (j/k) which are located next to the post title. Post navigation is a core WordPress feature and is present in all WordPress themes.

Why is post navigation important?

Post navigation is important because it allows users to move between posts in a WordPress site without having to return to the homepage or use the back button in their browser. This makes for a better user experience as they can easily navigate between posts without having to reload the page.

How to create custom post navigation in WordPress themes?

There are two ways to create custom post navigation in WordPress themes:

  • Using the next_post_link() and previous_post_link() functions.
  • Using the wp_link_pages() function.

The next_post_link() and previous_post_link() functions are the preferred method as they are more flexible and can be used to create custom post navigation menus. The wp_link_pages() function is less flexible and should only be used if you want to create a post navigation menu which contains links to all the pages in a post (i.e. if your post is split into multiple pages).

Using the next_post_link() and previous_post_link() functions

The next_post_link() and previous_post_link() functions can be used to create custom post navigation menus. These functions accept a number of parameters (listed below) which can be used to customize the post navigation menu.

  • next_post_link($format, $link, $in_same_cat, $excluded_categories)
  • previous_post_link($format, $link, $in_same_cat, $excluded_categories)

The $format parameter is used to specify the format of the post navigation menu. The $link parameter is used to specify the link text. The $in_same_cat parameter is used to specify whether the next/previous post should be in the same category as the current post. The $excluded_categories parameter is used to specify a comma-separated list of category IDs to exclude.

The code below shows how to use the next_post_link() and previous_post_link() functions to create a custom post navigation menu.

<?php
// Define the format of the post navigation menu
$format = '<div class="nav-link">%link</div>';

// Display the post navigation menu
next_post_link($format, 'Next post', true);
previous_post_link($format, 'Previous post', true);
?>

The code above will output the following post navigation menu:

Using the wp_link_pages() function

The wp_link_pages() function can be used to create a post navigation menu which contains links to all the pages in a post (i.e. if your post is split into multiple pages). This function accepts a number of parameters (listed below) which can be used to customize the post navigation menu.

  • wp_link_pages( $args )

The $args parameter is an array of arguments which can be used to customize the post navigation menu. The arguments which can be used are listed below.

  • before – The HTML to output before the post navigation menu.
  • after – The HTML to output after the post navigation menu.
  • link_before – The HTML to output before each link in the post navigation menu.
  • link_after – The HTML to output after each link in the post navigation menu.
  • next_or_number – Whether to display the “next” link or page numbers. Valid values are 'next' or 'number'.
  • separator – The HTML to output between the “next” link and page numbers.
  • nextpagelink – The text to use for the “next” link.
  • previouspagelink – The text to use for the “previous” link.
  • pagelink – The HTML to use for each page number link.
  • echo – Whether to echo the post navigation menu or return it as a string. Valid values are 0 (return as a string) or 1 (echo).

The code below shows how to use the wp_link_pages() function to create a custom post navigation menu.

<?php
// Define the args for the wp_link_pages() function
$args = array(
  'before' => '<div class="nav-link">',
  'after' => '</div>',
  'link_before' => '',
  'link_after' => '',
  'next_or_number' => 'next',
  'separator' => '|',
  'nextpagelink' => 'Next Page',
  'previouspagelink' => 'Previous Page',
  'pagelink' => '%',
  'echo' => 1
);

// Display the post navigation menu
wp_link_pages( $args );
?>

The code above will output the following post navigation menu: