Create a custom WordPress Pagination in less than 10 lines of code

Remi | August 8, 2012 | in Tutorials

There’s something i have never really liked in WordPress, it’s the “older posts” / “previous posts” links. Simply because a site using this “pagination” method doesn’t give any information to the visitor about the number of published posts. I prefer having a nice pagination that clearly shows that there are “x” posts, it gives me an idea about the website i’m currently visiting. That’s why i’m going to explain in the above lines how to create a simple but nice pagination in just a few lines of code.

The paginate_links() function is our saver

First of all you have to know something: WordPress core developers have nearly always a solution for us, and this is what i like in WordPress. There’s always a function, a bit of code on the codex, to help us creating great stuff. This is the case with the paginate_links() function. So, in the code above what we are doing is getting the total pages number that we put inside the $total variable. Then, we get the current page, and then the permalink format (don’t forget to set up a SEO friendly permalink structure). Just paste the PHP code where you want the pagination to appear and the CSS code inside your style.css file (in your theme directory), and that’s it ! You can view the result here or here!

The code

The author

About the author Remi: Hey! My name is Rémi Corson, I'm a French WordPress Developer, I code stuff and I write from time to time ;-)


  1. Lulupard


    It says “Can’t use function return value in write context in /wp-content/themes/testtheme/includes/template-tags.php on line 27”. That line is “if(empty(get_option(‘permalink_structure’))) {“

    1. Rémi Post author

      Hi, in that case, just replace

      if( empty(get_option(‘permalink_structure’)) ) {
      $format = ‘&page=%#%’;
      } else {
      $format = ‘page/%#%/’;


      $format = ‘&page=%#%’;

      and that should do it!

      1. Lulupard

        That way it does not work with default permalinks.
        I found out that this works with both (on WP 3.4.1)

        if (get_option(‘permalink_structure’)) {
        $format = ‘page/%#%/’;
        else {
        $format = ‘&paged=%#%’;

  2. Monica

    I agree, the next page link is boring! Thanks for this post… I’ve been developing a theme from scratch and this will come in SO handy. Can’t wait to go try it out!!! Thanks again =D

  3. Emmanuel

    Hi Remi ;-)

    I code this condition
    if( get_option(‘permalink_structure’) == ‘/%postname%/’ )

    because if permalink structure option is set to custom, I have a 404 error page when I click on page number links. The link generated was &paged=%#% instead of page/%#%/ which it’s expected.


  4. Julio Potier@Développement WordPress

    Hello Rémi

    I think there is a mess here :
    if( get_option(‘permalink_structure’) ) {
    $format = ‘&paged=%#%’;
    } else {
    $format = ‘page/%#%/’;

    1st: The “if” statement is reversed, if we’re using the permalink structure, we do not want the &paged
    2nd: the get_option is not the right way to check, you have to use $wp_rewrite->using_permalinks()
    3rd: the “&paged” leads on 404 page because of “&” instead of “?”, in fact you have to check is the $_GET array is empty or not, depending on that, you choose the right one
    4th: The “/page/” is not the right way to write the pagination base, you have to use $wp_rewrite->pagination_base

    So the comment from Emmanuel is not correct, he did not find that the IF was reversed.

    Thanks, see you later :)

  5. Ross

    Hi Remi – would it be possible for me to email you the code or some WP logins for you to take a look at? I think it is probably something simple to fix, but I’ve just spent so much time on it already that I don’t think I’m going to be able to solve it!

  6. Corey Lewis

    Remi, you made this the easiest code that I needed to apply to a site I am developing!!!!! Great work!

    The only thing I had to change in order for the pagination to work on my Custom Post Type was the following:

    $format = ‘&paged=%#%’;

    $format = ‘/page/%#%’;

    Thanks again!

  7. robertlim

    Don’t keep saying “Yes there is a bug and I should fix it !” if you are selling the software it is your obligation to fix it otherwise please don’t sell it or be upfront that what you sell does not fully work properly on pagination.

Leave a reply