Solution of custom rewrite rules for WordPress pagination

If you are looking for solution of custom WordPress pagination then you can check our previous post How to add custom pagination to WordPress and to learn about custom rewrite rules check the post How to add custom rewrite rules to WordPress. In this tutorial you will learn about custom rewrite rules for WordPress pagination. For example we have a portfolio page and table name is wpportfolio and we want to show 10 portfolio per page. So first step is to add the rewrite rules to WordPress. Just add the following code to your funtions.php

[php]</p>
function add_rewrite_rules($aRules)
{
$aNewRules = array(
‘portfolio/([^/]+)/?$’ => ‘index.php?pagename=portfolio&page=$matches[1]’,
‘top’
);
$aRules = $aNewRules + $aRules;
return $aRules;
}

add_filter(‘rewrite_rules_array’, ‘add_rewrite_rules’);
function add_query_vars($aVars) {
$aVars[] = "page";
return $aVars;
}
add_filter(‘query_vars’, ‘add_query_vars’);
[/php]

Next step is to construct a pagination class. Create a file name generate_pagination_class.php or you can set the name as your choice and save the following code:

[php]

<?php
$max = 10;
$page = $wp_query->query_vars[‘page’];
if($page==0)
{
$p = 1;
}
else
{
$p = $page;
}
$limit = ($p – 1) * $max;
$prev = $p – 1;
$next = $p + 1;
$limits = (int)($p – 1) * $max;

$table_name = $wpdb->prefix . "wpportfolio ";
$totalres = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name " );
$totalposts = ceil($totalres / $max);
$lpm1 = $totalposts – 1;
$targetpage = get_bloginfo(‘url’).’/portfolio’;

function pagination($totalposts,$p,$lpm1,$prev,$next,$targetpage){

$adjacents = 3;

if($totalposts > 1)
{

$pagination .= "<center><div class=\"pagination\">";
if ($p > 1)
$pagination.= "<a href=\"$targetpage/$prev/\">Previous</a> ";
else
$pagination.= "<span class=\"disabled\">Previous</span> ";
if ($totalposts < 7 + ($adjacents * 2)){
for ($counter = 1; $counter <= $totalposts; $counter++){
if ($counter == $p)
$pagination.= "<span class=\"current\">$counter</span>";
else
$pagination.= " <a href=\"$targetpage/$counter/\">$counter</a> ";}
}elseif($totalposts > 5 + ($adjacents * 2)){
if($p < 1 + ($adjacents * 2)){
for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++){
if ($counter == $p)
$pagination.= " <span class=\"current\">$counter</span> ";
else
$pagination.= " <a href=\"$targetpage/$counter/\">$counter</a> ";
}
$pagination.= " … ";
$pagination.= " <a href=\"$targetpage/$lpm1/\">$lpm1</a> ";
$pagination.= " <a href=\"$targetpage/$totalposts/\">$totalposts</a> ";
}
elseif($totalposts – ($adjacents * 2) > $p && $p > ($adjacents * 2)){
$pagination.= " <a href=\"$targetpage/1/\">1</a> ";
$pagination.= " <a href=\"$targetpage/2/\">2</a> ";
$pagination.= " … ";
for ($counter = $p – $adjacents; $counter <= $p + $adjacents; $counter++){
if ($counter == $p)
$pagination.= " <span class=\"current\">$counter</span> ";
else
$pagination.= " <a href=\"$targetpage/$counter/\">$counter</a> ";
}
$pagination.= " … ";
$pagination.= " <a href=\"$targetpage/$lpm1/\">$lpm1</a> ";
$pagination.= " <a href=\"$targetpage/$totalposts/\">$totalposts</a> ";
}else{
$pagination.= " <a href=\"$targetpage/1/\">1</a> ";
$pagination.= " <a href=\"$targetpage/2/\">2</a> ";
$pagination.= " … ";
for ($counter = $totalposts – (2 + ($adjacents * 2)); $counter <= $totalposts; $counter++){
if ($counter == $p)
$pagination.= " <span class=\"current\">$counter</span> ";
else
$pagination.= " <a href=\"$targetpage/$counter/\">$counter</a> ";
}
}
}
if ($p < $counter – 1)
$pagination.= " <a href=\"$targetpage/$next/\">Next</a>";
else
$pagination.= " <span class=\"disabled\">Next</span>";
$pagination.= "</center>\n";
}
return $pagination;
}
?>

[/php]

In line 2 we set we sen value 10 to $max variable to show 10 item per page. if you want different number then just change the value here. The we queried the query vars we have used during writing rewrite rules in funtions.php. In line 17 we set the table name. If you have different table name just change the name. Our page name is portfolio so we set target page portfolio in line 21.

Next step is to execute the WordPress query and here the query sample:

[html]<?php
global $wpdb,$wp_query;
$table_name = $wpdb->prefix . "wpportfolio";
include(‘generate_pagination_class.php’);
$retrieve_data = $wpdb->get_results( "SELECT * FROM $table_name LIMIT ".$limits.",".$max );
foreach ( $retrieve_data as $data )
{
retrive your data here…
}
?>
<div align="center" ><?php echo pagination($totalposts,$p,$lpm1,$prev,$next,$targetpage);?></div>[/html]

In line 4 we include the pagination class we have created and finally to show the pagination we use line 11. This will output a pagination link with following URl for page 2:

example.com/portfolio/2/

wordpress pagination expamle

Author: mytutorialguru

Share This Post On

Submit a Comment

Your email address will not be published. Required fields are marked *