How to implement WordPress pagination?

WordPress has nice feature to set the number of blog pages it shows for specified taxonomy.

Unfortunately, WordPress won’t paginate your pages automatically. You need to manually set the so called pager_fix.

Here is how to do it:

  1. Add the following function to functions.php file
function pager_fix($separator = ' | ', $after_previous = ' ', $before_next = ' ', $prelabel='« Previous Page', 
$nxtlabel='Next Page »', $current_page_tag = 'b'){
   global $posts_per_page, $paged, $wp_query;
   $numposts = $wp_query->found_posts;
   $max_num_pages = ceil($numposts / $posts_per_page);
   if ($max_num_pages > 1) {    
     for ($cnt = 1; $cnt <= $max_num_pages; $cnt++) {
         if ($current_page_tag && $paged == $cnt) {
             $begin_link = "<$current_page_tag>"; 
             $end_link = "<!--$current_page_tag-->";
         } else { $begin_link = ''; $end_link = ''; }
         $x[] = $begin_link . '<a href="' . get_pagenum_link($cnt) . '">' . 
   $cnt . '</a>' . $end_link;
     }
    echo join($separator, $x);
   }
}
  1. Add the following to the end of loop.php file:
<div id="pager">
  <?php if(pager_fix) pager_fix(" | ","","","","","strong");?>
</div>

The result:

tags: pagination & category: wordpress