Today, I'm going to show you how to add pagination with
Special recent post Plugin.
If you are using special recent post plugin to show recent
posts in your blog website.
You might have stuck with pagination problem i.e. what If you have 100 post and you want to show 10 posts per page?
You might have stuck with pagination problem i.e. what If you have 100 post and you want to show 10 posts per page?
I've made some custom changes in the special recent post plugin to made it work with pagination.
Follow these instructions to add pagination.
First we need to set the plugin is fetching the posts from
the database so we need to set the offset option into the queries option so we
can say what page number is requested and fetching from number of post up to 10
or whatever post you need to show in one page.
Go to the "special recent posts" plugin now go to class directory of it. You'll
find a file
class-main.php
Now find the function
This is where it the code of posts fetching is written.
getPosts()
This is where it the code of posts fetching is written.
Now take backup of file and replace following function with
the original function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private function getPosts() { | |
$c_offset = (isset($_GET['page'])) ? ($_GET['page'] * 5) - 5 : 0 ; | |
// Defining args array. | |
$args = array ( | |
'post_type' => $this->widget_args["post_type"], | |
'numberposts' => ($this->widget_args["post_limit"] * $this->plugin_args["srp_global_post_limit"]), | |
'post_status' => $this->widget_args["post_status"], | |
'offset' => $c_offset | |
); | |
// Checking for Compatibility Mode. | |
if ($this->plugin_args["srp_compatibility_mode"] == 'yes') { | |
// Compatibility mode filter. This might cause unknown problems. Deactivate it just in case. | |
$args["suppress_filters"] = false; | |
} | |
// Checking for post order option. | |
switch ($this->widget_args["post_order"]) { | |
case "ASC": | |
case "DESC": | |
// Ordering posts by ASC/DESC order | |
$args["order"] = $this->widget_args["post_order"]; | |
break; | |
default: | |
// Default behaviour: ordering by DESC. | |
$args["order"] = "DESC"; | |
break; | |
} | |
// Checking for custom post type option. | |
if ($this->widget_args["custom_post_type"] != '') { | |
// Filtering result posts by category ID. | |
$args["post_type"] = $this->widget_args["custom_post_type"]; | |
} | |
// Checking if category filter is applied. | |
if ($this->widget_args["category_include"] != '') { | |
// Filtering result posts by category ID. | |
$args["category"] = $this->widget_args["category_include"]; | |
} | |
// Checking if "post current hide" option is enabled. | |
if ($this->widget_args["post_current_hide"] == 'yes') { | |
// Filtering current post from visualization. | |
$args["exclude"] = $this->singleID; | |
} | |
// Check if post offset option is enabled. | |
if ($this->widget_args["post_offset"] != 0) { | |
// Applying post offset. | |
$args["offset"] = $this->widget_args["post_offset"]; | |
} | |
// Checking if exclude posts option is applied. | |
if (!empty($this->widget_args["post_exclude"])) { | |
// Excluding result posts by post IDs. | |
$args["exclude"] = $this->widget_args["post_exclude"]; | |
} | |
// Checking if include posts option is applied. | |
if (!empty($this->widget_args["post_include"])) { | |
// Including result posts by post IDs. | |
$args["include"] = $this->widget_args["post_include"]; | |
} | |
// Calling built-in Wordpress 'get_posts' function. | |
$result_posts = get_posts($args); | |
// Checking if result posts array is empty. | |
if (empty($result_posts)) { | |
// No recent posts available. Return empty array. | |
return $result_posts; | |
} | |
// Looping through result posts for image check. | |
// If "skip posts with no image" option is enabled, re-build entire array, matching the post limit. | |
foreach($result_posts as $k => $v) { | |
// Filling thumb list with post IDs as keys. | |
$currentThumbHtml = $this->displayThumb($v); | |
// If the current post has no images and the "skip posts with no image" option is enabled, remove the post from the list. | |
if (!$currentThumbHtml) { | |
unset($result_posts[$k]); | |
} else { | |
// Push the current post thumbnail HTML in the global thumbnail output list. | |
$this->postThumbsOutput[$v->ID] = $currentThumbHtml; | |
} | |
} | |
// Fixing issues that let included IDs override the max number of post displayed. | |
$output_array = array_slice($result_posts, 0, $this->widget_args["post_limit"]); | |
// Checking if random posts option is on. | |
if ($this->widget_args["post_random"] == "yes") { | |
// Shuffling the result array. | |
shuffle($output_array); | |
} | |
// Return result array. | |
return $output_array; | |
} |
Now go to the your current theme write it open the template file of your blog page.
Replace this code after the end of the while loop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private function getPosts() { | |
$c_offset = (isset($_GET['page'])) ? ($_GET['page'] * 5) - 5 : 0 ; | |
// Defining args array. | |
$args = array ( | |
'post_type' => $this->widget_args["post_type"], | |
'numberposts' => ($this->widget_args["post_limit"] * $this->plugin_args["srp_global_post_limit"]), | |
'post_status' => $this->widget_args["post_status"], | |
'offset' => $c_offset | |
); | |
// Checking for Compatibility Mode. | |
if ($this->plugin_args["srp_compatibility_mode"] == 'yes') { | |
// Compatibility mode filter. This might cause unknown problems. Deactivate it just in case. | |
$args["suppress_filters"] = false; | |
} | |
// Checking for post order option. | |
switch ($this->widget_args["post_order"]) { | |
case "ASC": | |
case "DESC": | |
// Ordering posts by ASC/DESC order | |
$args["order"] = $this->widget_args["post_order"]; | |
break; | |
default: | |
// Default behaviour: ordering by DESC. | |
$args["order"] = "DESC"; | |
break; | |
} | |
// Checking for custom post type option. | |
if ($this->widget_args["custom_post_type"] != '') { | |
// Filtering result posts by category ID. | |
$args["post_type"] = $this->widget_args["custom_post_type"]; | |
} | |
// Checking if category filter is applied. | |
if ($this->widget_args["category_include"] != '') { | |
// Filtering result posts by category ID. | |
$args["category"] = $this->widget_args["category_include"]; | |
} | |
// Checking if "post current hide" option is enabled. | |
if ($this->widget_args["post_current_hide"] == 'yes') { | |
// Filtering current post from visualization. | |
$args["exclude"] = $this->singleID; | |
} | |
// Check if post offset option is enabled. | |
if ($this->widget_args["post_offset"] != 0) { | |
// Applying post offset. | |
$args["offset"] = $this->widget_args["post_offset"]; | |
} | |
// Checking if exclude posts option is applied. | |
if (!empty($this->widget_args["post_exclude"])) { | |
// Excluding result posts by post IDs. | |
$args["exclude"] = $this->widget_args["post_exclude"]; | |
} | |
// Checking if include posts option is applied. | |
if (!empty($this->widget_args["post_include"])) { | |
// Including result posts by post IDs. | |
$args["include"] = $this->widget_args["post_include"]; | |
} | |
// Calling built-in Wordpress 'get_posts' function. | |
$result_posts = get_posts($args); | |
// Checking if result posts array is empty. | |
if (empty($result_posts)) { | |
// No recent posts available. Return empty array. | |
return $result_posts; | |
} | |
// Looping through result posts for image check. | |
// If "skip posts with no image" option is enabled, re-build entire array, matching the post limit. | |
foreach($result_posts as $k => $v) { | |
// Filling thumb list with post IDs as keys. | |
$currentThumbHtml = $this->displayThumb($v); | |
// If the current post has no images and the "skip posts with no image" option is enabled, remove the post from the list. | |
if (!$currentThumbHtml) { | |
unset($result_posts[$k]); | |
} else { | |
// Push the current post thumbnail HTML in the global thumbnail output list. | |
$this->postThumbsOutput[$v->ID] = $currentThumbHtml; | |
} | |
} | |
// Fixing issues that let included IDs override the max number of post displayed. | |
$output_array = array_slice($result_posts, 0, $this->widget_args["post_limit"]); | |
// Checking if random posts option is on. | |
if ($this->widget_args["post_random"] == "yes") { | |
// Shuffling the result array. | |
shuffle($output_array); | |
} | |
// Return result array. | |
return $output_array; | |
} |
Now check the your blog page. The pagination should be working.
Apply style as per your theme.
That's it. Easy isn't it?
Subscribe to my blog and stay updated with more tips and
tricks.
No comments:
Post a Comment