Using WordPress ‘posts_nav_link()’ PHP function

The posts_nav_link() WordPress PHP function displays the post pages link navigation for previous and next pages.

Usage

posts_nav_link( $sep, $prelabel, $nxtlabel );

Parameters

  • $sep string (Optional): Separator for posts navigation links. Default: ”
  • $prelabel string (Optional): Label for previous pages. Default: ”
  • $nxtlabel string (Optional): Optional Label for next pages. Default: ”

More information

See WordPress Developer Resources: posts_nav_link

Examples

Using Images

In this example, we replace the text with custom images for previous and next page links.

posts_nav_link( ' ', '<img src="' . esc_attr( get_bloginfo( 'stylesheet_directory' ) . '/images/prev.jpg' ) . '" />', '<img src="' . esc_attr( get_bloginfo( 'stylesheet_directory' ) . '/images/next.jpg' ) . '" />' );

Default Usage

By default, the posts_nav_link() looks like this: « Previous Page — Next Page »

posts_nav_link();

Displays previous and next page links (“previous page · next page”) centered on the page.

<div style="text-align:center;">
    posts_nav_link( ' · ', 'previous page', 'next page' );
</div>

You can change the text in each of the links and in the text in between the links. This example changes the default text for previous and next pages.

<p>
    posts_nav_link( ' or ', 'You can go back to the previous page', 'you can go forward to the next page' );
</p>

Kubrick Theme Format (Better Code)

The Kubrick theme format for posts navigation, could be formatted this way. This example uses previous_posts_link() and next_posts_link() functions instead.

<div class="navigation">
    <div class="alignleft">
        previous_posts_link( '&laquo; Previous Entries' );
    </div>
    <div class="alignright">
        next_posts_link( 'Next Entries &raquo;', '' );
    </div>
</div>