How to add posts_nav_link() to WordPress theme?

Building WP theme from scratch! You’ll definitely need to add pagination in the index, archives, and similar templates and you can do this by adding the posts_nav_link function. Here is how to add them.

Note: I assume that you have knowledge of PHP and where to add these codes.

Default pagination:

<?php posts_nav_link(); ?>

It would look like this:

« Previous PageNext Page »

Customizing the default pagination:

We can make it look better by wrapping it in a class.

<div class="navigation">
    <?php posts_nav_link('—','&larr; New Entries','Old Entries &rarr;'); ?>

And some CSS values to make it look better…

.navigation { font-size: 1.25rem; text-align: center; }

After some CSS customization, it would look like this:

← New EntriesOld Entries →

Here you can add three parameters inside the function which are a separator, previous label, and next label. You can customize the words with any HTML Unicode symbol or text.

Further, you need to beautify the “navigation” class using CSS.

Leave a Reply

Your email address will not be published. Required fields are marked *