Custom Paging in WordPress

It is sometimes required in WordPress that you need to include your own paging.This is how i have done it.

$rss = fetch_feed($feed_url);
if (!is_wp_error( $rss ) ) {
$limit = 15;

$page = $_GET['page'];
if ((!$page) || (is_numeric($page) == false) || ($page < 0)) {

$page = 1; //default

}

$set_limit = $page * $limit - ($limit);

$prev_page = $page-1;

$next_page = $page+1;

$total_item=$rss->get_item_quantity(); //PUT HERE YOUR TOTAL NUMBER OF RESULTS

$total_pages = ceil($total_item / $limit);

if ($total_item == 0) {
$content_start = 0;
} else {
$content_start = $set_limit +1;
}

$content_end = (($limit*$page)>=$total_item)?$total_item:($limit*$page);
$start_page = ($page<9)?1:($page-5);
$end_page = (($start_page+9)<$total_pages)?($start_page+9):$total_pages;
$rss_items = $rss->get_items($set_limit, $limit);
$array = array();// now we can iterate through all results
$main_array = array();
$page_result = 1;
$start_limit = $limit * ($page - 1) + 1;
$end_limit = $start_limit + $limit;

//PUT YOUR CODE HERE

if ($total_pages > 1) { ?>
<div style="padding-bottom:20px;">
<span>Page <?php echo $page;?> of <?php echo $total_pages; ?></span> <?php
if ($page != 1) { ?>
<a href="?page=<?=$prev_page?>"><<</a> <?php
}
for ($count=1; $count<=$total_pages; $count++) {
if ($count == $page) { ?>
<span><?php echo $count; ?></span> <?php
} else { ?>
<a href="?page=<?=$count?>" ><?php echo $count; ?></a> <?php
}
}
if ($page != $total_pages) { ?>
<a href="?page=<?php echo $next_page; ?>">>></a> <?php
} ?>
</div> <?php
} else {
echo "No result found.";
}
Advertisements

About Kapil Chugh

In love with PHP and tweaking WordPress. Hungry for Open Source and new technologies. Honest. God-fearing. Happiest when solving problems.
This entry was posted in WordPress and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s