Easy WordPress pagination without plugins

Posted on April 6th, 2012. Written by Blancer.

Simply paste the following code where you want to display your pagination:

global $wp_query;
$total = $wp_query->max_num_pages;
// only bother with the rest if we have more than 1 page!
if ( $total > 1 )  {
     // get the current page
     if ( !$current_page = get_query_var('paged') )
          $current_page = 1;
     // structure of "format" depends on whether we're using pretty permalinks
     $format = empty( get_option('permalink_structure') ) ? '&page=%#%' : 'page/%#%/';
     echo paginate_links(array(
          'base' => get_pagenum_link(1) . '%_%',
          'format' => $format,
          'current' => $current_page,
          'total' => $total,
          'mid_size' => 4,
          'type' => 'list'
     ));
}

Thanks to Smashing Magazine for the cool tip!

This entry was posted on Friday, April 6th, 2012 at 8:03 pm and is filed under Wordpress. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site. Both comments and pings are currently closed.

Blancer

Similar Projects

    None Found