Custom Pagination for your WordPress themes

I was recently busy building a new wordpress theme and I decided that I would like to get a better way of showing pagination on my theme pages.

Usually you get the normal wordpress pagination with the next and previous links at top and/or the bottom of you blogging page which really is awful in my opinion. I wanted something to work for me so started Googeling around to find a proper solution.

A lot of people recommended integrating an existing plugin called WP-PageNavi, but all the posts I saw was a bit outdated and the plugin was already at version 2.8. So I decided to find a different solution that was easy to integrate and adapt if needed.

Part of the solution started with a PHP class called “Digg Style Pagination Class” which is easy to use and implement, but I needed to add some code for this to work correctly on wordpress themes.

If you downloaded the Pagination Class then the code below is how you would use it.

function mypager($num) {

$count_posts = wp_count_posts();

$totalposts = $count_posts->publish;

$totalpages = $totalposts / $num;

include 'pagination.class.php';

$urld = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']; // Get the current URL

$currentpage = preg_replace('/[^0-9$]/', '',  $urld); //get the page number

$permalink = preg_replace('/\/page\/[0-9]/', '',$urld); //set custom permalink

if(empty($currentpage)){$currentpage = 1;}

$p = new pagination;

$p->items($totalposts);

$p->limit($num);

$p->currentPage($currentpage);

$p->parameterName("p");

$p->urlFriendly();

$p->target($permalink."page/%/");//#page/1/

$p->show();

}

Let  me break it down for you.

First we start by creating a function called mypager($num).

Next we need to get the posts count to get the total amount of posts for the current loop and then divide the total by the amount of posts we are showing per page.  This is how we do it:

$count_posts = wp_count_posts();

$totalposts = $count_posts->publish;

$totalpages = $totalposts / $num;

Next we include the pagination class:

include 'pagination.class.php';

Next we need to get the current page URL so that we can set the correct pagination number:

$urld = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

Next we need to do an str_replace to get the page number we are on:

$currentpage = preg_replace('/[^0-9$]/', '',  $urld);

And then we need to create our own little permalink variable like so:

$permalink = preg_replace('/\/page\/[0-9]/', '',$urld);

Our next step is to check if the current page should be page 1 or not:

if(empty($currentpage)){$currentpage = 1;}

All that is left was to set the pagination class variables to call the correct way and display the numbers:

$p = new pagination;

$p->items($totalposts);

$p->limit($num);

$p->currentPage($currentpage);

$p->parameterName("p");

$p->urlFriendly();

$p->target($permalink."page/%/");//#page/1/

$p->show(); 

In the end all you need to do is add a function call to the end of your page where the pagination is needed like so:

<?php if(function_exists('zkrpager')) { zkrpager(10); } ?>

Please keep in mind that you need to set the correct number in the function call so that it will display the correct amount of numbers. WordPress normally sets it to 10 but you could change it for example when you have written a custom loop where you would like to display custom post types and only 6 posts per page.

As you can see this was very easy to use implement let me know if this works for you and if you can add on to the code please comment below.

mypager

Related Posts:

  • rss
  • email
  • rss
  • email
I'm a full time PHP developer and I just love all things web related. If you need help I'm your guy.
  • Jane doe

    The Digg Style Pagination class link doesn’t have the code. Where can we get the code for the class

  • http://teachingyou.net Morné

    Hi there, if you went to the digg style pagination link you would of seen the download link. Here is the link anyways http://www.mis-algoritmos.com/wp-content/uploads/pagination.class041.rar Thanks for your comment.

  • Mushtaq Wani

    i downloaded the code of digg style pagination from the link given in the comment below. and extracted the zip file named pagination.class and i have also uploaded the same to server . now i cant understand where to put the code which is shown above in order to make function it ..
    plz help

  • http://teachingyou.net Morné

    Hi there,

    you can copy the complete code to your functions.php file as it is a complete function I’ve written. Just make sure that your pagination class gets included via php include function in your functions.php file too.

    you should then be able to call it in you template file like this

    if(function_exists(‘zkrpager’)) { zkrpager(10); }

    Hope that helps.

  • http://www.creativementor.com.au/wordpress-training-course.html wordpress training

    Custom pagination is useful feature in wordpress to move pages from first to last. It might be easy to transfer. I like this post and i also try to do this type of pagination in my wordpress site so this is helpful for me.

  • http://livechat.comm100.com/wordpress-chat-plugin.aspx wordpress chat

    Custom pagination is one of the best way to move from first page to last page using the above code. Digg style is easy to use and also works perfectly on WordPress sites.

  • Aliza Martin Joe

    Really helpful man. I did this way and got the results. Thanks here is the URL of the website i found while searching. It was really a help for me also. http://www.scratchinginfo.net/