Post Pic

Easy WordPress pagination without plugins

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!

11 Responses

Apr 16 2012 08:43

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?

Apr 18 2012 04:05

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.

Apr 18 2012 04:12

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

Apr 18 2012 10:11

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.

May 09 2012 16:12

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

May 16 2012 22:24

Really cool tip, thanks for sharing it!

Jun 29 2012 11:21

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

Thanks!

Jul 27 2012 13:14

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

Feb 14 2013 04:07

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

Mar 29 2013 12:03

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/%#%/’;

Apr 15 2013 10:26

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/%#%/’;

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox