Pagination Links in WordPress via Functions.php

It took me awhile to find a simple script to drop into my functions.php file which could allow me to then output pagination links. You know the type, they look like this:

a screenshot of paginated links, showing an older link, various numbers representing different pages, and then a newer link.
Here we have older / newer links, as well as numbered pagination links to skip to specific pages.

I’ll explain how this code came to be and why below, but if you’d like, you can just skip down and get the code for your functions.php file.

I think a solid pagination function does the following:

  1. Only shows a few numbers around the first page. After a few years, a blog may have 100s of pages to scroll through, so no reason to show 100s of links to those pages. Instead, we’ll show the page we’re on, two pages surrounding it in either direction, and then links to the first and last pages.
  2. Has Previous and Next links so that users can just click back to the page before the one they’ve been reading, which keeps it as simple as possible for those who are just reading way back whens.

Basically what we do with the code below is check how many pages we have, and if it’s more than 5, we start showing the various first, last and next/previous links mentioned and shown above. If there are less than 5, we simply show those 2-4 links. You can change the $range value in the first line to whatever you’d like to increase the number of links that are shown adjacent to the current page. So set it to $range = 3 and you’ll get three newer pages to the right and 3 older pages to the left of our current page’s marker in the pagination display.

The code for your functions.php file:

function pagination($pages = '', $range = 2) {
$morepages = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '') {
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages) {
$pages = 1;
if(1 != $pages) {
echo '<div class="pagination">';
if($paged > 1 && $morepages < $pages) echo '<a class="prev-link" href="'.get_pagenum_link($paged - 1).'">&larr;</a>';
if($paged > 2 && $paged > $range+1 && $morepages < $pages) echo '<a href="'.get_pagenum_link(1).'">1</a><span class="separate">...</span>';
for ($i=1; $i <= $pages; $i++) { if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $morepages )) { echo ($paged == $i)? '<span class="current">'.$i.'</span>':'<a href="'.get_pagenum_link($i).'">'.$i.'</a>';
if ($paged < $pages-1 && $paged+$range-1 < $pages && $morepages < $pages) echo '<span class="separate">...</span><a class="last-link" href="'.get_pagenum_link($pages).'">'.$pages.'</a>';
if ($paged < $pages && $morepages < $pages) echo '<a class="next-link" href="'.get_pagenum_link($paged + 1).'">&rarr;</a>';
echo '</div>';

And then drop this into your theme where appropriate (usually on an index.php, archive.php, category.php, search.php file):

<?php pagination(); ?>

That’s the default implementation and will show links to two pages around the current page, as well as the first page and last page links, and the prev/next links.

But you can also define some of these properties right when we call the function:

<?php pagination('','3'); ?>

This will show three links around the current page.

<?php pagination('5'); ?>

This will simply show 5 links, from page 1 through 5. Useful I suppose if you don’t want people digging too far into the past.

Up Next: How to Create a Settings Page for Your WordPress Theme