WordPress Pagination without Plugins

wordpress-pagination

For a very long time, now, WP-PageNavi has been a very popular plugin to use for pagination. It’s well built, easy to customize and easy to add to your theme. Plus, it handles pagination for everything — post pages, comments and post archives.

But, did you know WordPress has this function already built-in? It’s just takin some time for it to be unearthed, and hopefully after learning how to use the WordPress function, you can start using it and give yourself one less plugin to deal with!

WordPress’ Built-In Pagination

The function is paginate_links(). Have you heard of it?

You can find the source code on line 1954 of your general-template.php in the wp-includes folder since 3.1. This function has actually been available since 2.1 (paginate_comment_links() has been around since 2.7).

It’s about time we started using it!

Drop this code snippet in your templete:

// get total number of pages
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'
     ));
}

You can get more deets from the WordPress codex as well as a more in-depth look on Smashing Magazine.

[via Smashing Magazine]

5 SHARES

Eric Dye

I am a blogger, business owner and lover of coffee. I spend most of my time as writer and editor for ChurchMag, but you can also find me working on Live Theme and ChurchMag Press. All while enjoying my family and sipping espresso in Italy.

What would you like to say?

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>