img Hey! I'm a French WordPress developer! Welcome on my site!
  • Follow me:
Create a custom WordPress Pagination in less than 10 lines of code
8th Aug

2012

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

Share This :

Hey! My name is Rémi Corson, i'm a WordPress Expert Developer, i create high level themes and plugins, and i provide free stuff every week on this website!

Comments

Lulupard

2012-08-16 13:54:11 Reply

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’))) {“

    Rémi

    2012-08-16 17:44:44 Reply

    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!

      Lulupard

      2012-08-17 00:19:25 Reply

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

        Rémi

        2012-08-17 07:40:01 Reply

        Thanks! I update the code!

Monica

2012-08-16 14:47:09 Reply

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

    Rémi

    2012-08-16 17:45:56 Reply

    Great, good luck with your future devs! Maybe you’ll have to modify the $format variable, as explained in comment above.

Emmanuel

2013-05-15 20:17:46 Reply

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

    Remi

    2013-05-16 09:39:29 Reply

    Yes that’s correct, i should modify the code

Leave a Comment

Your email address will not be published.