DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.

WP-PageNavi is definitely one of the most popular WordPress plugins and in fact, it is very useful. But did you know that since version 2.1, WordPress had a built-in function called paginate_links(), which can be used to create to paginate your blog without using any plugin? Today, let’s see how to use this handy function to create a pagination for your WordPress blog.

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!

Tags

13 Comments

  1. Hi. This does not work for me. I tried on 2 blogs. I even tried with the default theme just in case. I paste it exactly where the old pagination links were, but just a blank page appears. WP version is 2.9.2. Did anyone made this code work?

  2. It looks like there is an error

    Can’t use function return value in write contex

    I change
    $format = empty( get_option(‘permalink_structure’) ) ? ‘&page=%#%’ : ‘page/%#%/’;

    to

    $format = get_option(‘permalink_structure’) ? ‘&page=%#%’ : ‘page/%#%/’;

    and it works. Just want to know how I can highlight the current page.

  3. Sorry, I know how to highlight the current page number now, should have read the document for paginate_links more carefully.

  4. Thanks Sola, now it kind of works. At first I needed to change the single quotes because the ones in your comment give error. And the other thing is that it makes the link like this /page/2/ no matter if I am using another link structure.

  5. Hi Nikolay, I have played around with the paginate_links function for a while after my last comment. It’s a really cool function, I think your problem has something to do with the $format.

    $permalink_structure = get_option(‘permalink_structure’);
    format = empty( $permalink_structure) ? ‘&page=%#%’ : ‘page/%#%/’;

    pay attention to ‘page/%#%/’, this controls the link structure

  6. Really cool tip, thanks for sharing it!

  7. It works very well and looks much better than the WordPress link “Older Posts”.

    Thanks!

  8. This is a pretty cool tip! Thanks – I will implement this. Can the code be wrapped in divs for css?

  9. Good, i like this.. This is a smart cool tip

  10. Not working!
    I got an error: Fatal error: Can’t use function return value in write context in this line – $format = empty( get_option(‘permalink_structure’) ) ? ‘&page=%#%’ : ‘page/%#%/’;

  11. Look the comments from Sola to solve the error, Ken

    change
    $format = empty( get_option(‘permalink_structure’) ) ? ‘&page=%#%’ : ‘page/%#%/’;

    to

    $format = get_option(‘permalink_structure’) ? ‘&page=%#%’ : ‘page/%#%/’;

  12. What this function to reverse. What would the numbering was not 12345 and 54321

  13. How to reverse this function? It is necessary that the numbering was the opposite 5-4-3-2-1

Leave a Comment

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