How to add pagination to WordPress posts

Pagination for your posts is pretty much essential for most websites, as it presents a simple, intuitive way for people to access your older blog/news/etc posts without having to search or follow individual links. However, it’s not enabled ‘out of the box’ when you are building your own theme, and while plugins can add this functionality for you, it’s often nicer to stick with some simple PHP code to do the work for you. The good news is that actually, WordPress has done the bulk of the work already, and under the bonnet offers some great pagination functionality which you just need to enable.

The first thing you probably want to do is remove any queries relating to post limits on the page that you want to paginate, e.g. this:

<?php query_posts( array( 'posts_per_page' => 5 ) ); ?>

That will enable WordPress’s own post limit to take effect – you can set this on the Settings -> Reading page, on the ‘Blog pages show at most’ setting. Once you have set this to a manageable number, we then just need to add a bit of code to your functions.php file:

function pagination_bar() {
global $wp_query;

$total_pages = $wp_query->max_num_pages;

if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));

echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,

Now this is done, pagination is all set up and ready to be used – you just need to pull it in on the relevant page template. Find the template for whichever page you want to paginate, and add the following code somewhere underneath your posts query:

<nav class="pagination">
<?php pagination_bar(); ?>

That should be it! You can then use the nav.pagination class to style the pagination area however you wish.