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

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

Liked it? Keep me awake with a coffee!

20 thoughts on “Create a custom WordPress Pagination in less than 10 lines of code

  1. Hi!

    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. Hi, in that case, just replace

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

      by

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

      and that should do it!

      1. 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. 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.

    Manu

  3. 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 :)

  4. 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!

  5. 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:

    FROM:
    $format = ‘&paged=%#%';

    TO:
    $format = ‘/page/%#%';

    Thanks again!

  6. 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