WordPress Snippets at WPcustoms

Pagination for WordPress and Bootstrap

This is my most used pagination snippet. It’s easy to read and to customize its html structure. Bootstrap is not required to make it work. There are many other pagination plugins around but either they are not easy to customize, require 20 different files or simply don’t work. This WordPress pagination snippet is a keeper.


/**
 * Snippet Name: Pagination for WordPress and Bootstrap
 * Snippet URL: http://www.wpcustoms.net/snippets/pagination-for-wordpress-and-bootstrap/
 */
 // usage:
if ($wp_query->max_num_pages > 1) : 
wpc_pagination();
 endif; 



function wpc_pagination($pages = '', $range = 2)
{
      $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 '
    '; if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo '
  • FIRST
  • '; if($paged > 1 && $showitems < $pages) echo '
  • '; for ($i=1; $i <= $pages; $i++) { if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) { echo ($paged == $i)? '
  • '. $i .'
  • ':'
  • '.$i.'
  • '; } } if ($paged < $pages && $showitems < $pages) echo '
  • '; if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo '
  • LAST
  • '; echo '
'; } }