WordPress : Creating Custom Pagination In Code

Hey all, this week’s been a bit of a change for me as I’ve been back to roots playing with PHP for a client.  They had a requirement to make a news page in WordPress that had Google like search pagination for their news and jobs categories.

Out of the box, WordPress only comes with the next and previous links so I thought I’d knock-up a quick pagination control.  The first thing any pagination control needs is the total number of posts for that category, I did this using this snippet:

 
$postsInCat = get_term_by('name', $catname,'category');
$totalPosts = $postsInCat->count;

Next, I needed to know the number of pages to display per page. For this I used the default posts_per_page option in wordpress. You can change this value yourself by logging into your admin and changing your URL to ‘wp-admin/options-general.php’. This will display a list of site options available to you. You can then get any of these options from a page by using this :

 
$postsperpage = get_option('posts_per_page');

The next step was to read the current page from the query-string   I do this in $paged variable. Next I call the WordPress API to get the posts I want for the given page I’m on and display the results.

The last part of the code snippet deals with the pagination. This first statement checks the number of posts per page has been set to a value greater than 0.  If it hasn’t, then the server will throw a divide by 0 error.  If the value is greater than 0 then the page figures out the total number of pages to display in the pagination, using :

     $numSearchFilters = $totalPosts / $postsperpage;

If all the results fit within one page, then the pagination isn’t displayed as it’s not needed.  If there is more than one page of results, then I use a simple loop to display all the pages, setting an active class for the page that matches the current page.

 
// Get's the total number of pages in results (used for pagintation page filters)
$postsInCat = get_term_by('name', $catname,'category');
$totalPosts = $postsInCat->count;
$postsperpage = get_option('posts_per_page');
// Used for the pagination
$paged = 1;
if ( get_query_var('paged') ) 
{
$paged = get_query_var('paged');
} 
else if ( get_query_var('page') ) 
{
$paged = get_query_var('page');
} 
$cat=get_cat_ID($catname); 
$args = array('cat' => $cat, 'paged' => $paged, 'orderby' => 'date', 'order' => 'ASC');
$posts = new WP_Query($args);
if($posts->have_posts()) : 
while($posts->have_posts()) : 
$posts->the_post();
// Display posts here
  0) :
$numSearchFilters = $totalPosts / $postsperpage;
if ($numSearchFilters > 1)  :  ?>

As always the full including the mark-up is here.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts