X

How to Enhance WordPress Pagination

Snippets by IsItWP

Are you looking for a way to enhance WordPress navigation? While there’s probably a plugin for this, we have created a quick code snippet that you can use to enhance WordPress navigation.

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

/**
 * Pagination - Add the proceeding to your functions.php file
 * You do not need to include the php opening and closing tags if pasting into your functions.php file
*/
function pagination($pages = '', $range = 1)
{  
     $showitems = ($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"><span>Page ".$paged." of ".$pages."</span>";
         if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>";
 
         for ($i=1; $i <= $pages; $i++)
         {
             if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
             {
                 echo ($paged == $i)? "<span class="current">".$i."</span>":"<a href='".get_pagenum_link($i)."' class="inactive">".$i."</a>";
             }
         }
 
         if ($paged < $pages && $showitems < $pages) echo "<a href="".get_pagenum_link($paged + 1)."">›</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
         echo "</div>n";
     }
}

Add the following code where you want to display the pagination:

<?php if (function_exists("pagination")) {
    pagination($additional_loop->max_num_pages);
} ?>

Add this CSS in your stylesheet.

.pagination {
clear:both;
padding:20px 0;
position:relative;
font-size:11px;
line-height:13px;
}
 
.pagination a {
display:block;
float:left;
margin: 3px;
padding: 5px;
width: 25px;
height: 25px;
border-radius: 5px;
text-align: center;
line-height: 24px;
text-decoration:none;
color:#fff;
background: #282828;
}

.pagination span {
display:block;
float:left;
margin: 3px;
padding:5px  10px;
height: 25px;
border-radius: 5px;
text-align: center;
line-height: 24px;
width:auto;
text-decoration:none;
color:#fff;
background: #282828;
}
 
.pagination .current{
margin: 3px;
padding: 5px;
width: 25px;
height: 25px;
border-radius: 5px;
text-align: center;
line-height: 22px;
background: #424242;
color:#fff;
}

.pagination a:link{
color:#fff;
}

.pagination a:visited{
color:#fff;
}

.pagination a:hover{
color:#fff;
background-color: #424242;
}

.pagination a:active{
color:#fff;
}

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly add code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 9 best WordPress events plugins and how to set up author tracking in WordPress with Google Analytics.

Comments   Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!