Custom Pagination WordPress

Generally we use the worpdress default pagination for our wordpress blog but here I find and write the code for Custom pagination for our blog page. First of all open the functions.php file of the current activated theme of the wordpress. Then add the below code into it as function.

[php]

request;
//intval — Get the integer value of a variable
/*http://php.net/manual/en/function.intval.php*/
$posts_per_page = intval(get_query_var(‘posts_per_page’));
//Retrieve variable in the WP_Query class.
/*http://codex.wordpress.org/Function_Reference/get_query_var*/
$paged = intval(get_query_var(‘paged’));
$numposts = $wp_query->found_posts;
$max_page = $wp_query->max_num_pages;

//empty — Determine whether a variable is empty
/*http://php.net/manual/en/function.empty.php*/
if(empty($paged) || $paged == 0) {
$paged = 1;
}

$pages_to_show = intval($custom_pagination[‘num_pages’]);
$larger_page_to_show = intval($custom_pagination[‘num_larger_page_numbers’]);
$larger_page_multiple = intval($custom_pagination[‘larger_page_numbers_multiple’]);
$pages_to_show_minus_1 = $pages_to_show – 1;
$half_page_start = floor($pages_to_show_minus_1/2);
//ceil — Round fractions up (http://us2.php.net/manual/en/function.ceil.php)
$half_page_end = ceil($pages_to_show_minus_1/2);
$start_page = $paged – $half_page_start;

if($start_page <= 0) { $start_page = 1; } $end_page = $paged + $half_page_end; if(($end_page - $start_page) != $pages_to_show_minus_1) { $end_page = $start_page + $pages_to_show_minus_1; } if($end_page > $max_page) {
$start_page = $max_page – $pages_to_show_minus_1;
$end_page = $max_page;
}
if($start_page <= 0) { $start_page = 1; } $larger_per_page = $larger_page_to_show*$larger_page_multiple; //round_num() custom function - Rounds To The Nearest Value. $larger_start_page_start = (round_num($start_page, 10) + $larger_page_multiple) - $larger_per_page; $larger_start_page_end = round_num($start_page, 10) + $larger_page_multiple; $larger_end_page_start = round_num($end_page, 10) + $larger_page_multiple; $larger_end_page_end = round_num($end_page, 10) + ($larger_per_page); if($larger_start_page_end - $larger_page_multiple == $start_page) { $larger_start_page_start = $larger_start_page_start - $larger_page_multiple; $larger_start_page_end = $larger_start_page_end - $larger_page_multiple; } if($larger_start_page_start <= 0) { $larger_start_page_start = $larger_page_multiple; } if($larger_start_page_end > $max_page) {
$larger_start_page_end = $max_page;
}
if($larger_end_page_end > $max_page) {
$larger_end_page_end = $max_page;
}
if($max_page > 1 || intval($custom_pagination[‘always_show’]) == 1) {
/*http://php.net/manual/en/function.str-replace.php */
/*number_format_i18n(): Converts integer number to format based on locale (wp-includes/functions.php*/
$pages_text = str_replace(“%CURRENT_PAGE%”, number_format_i18n($paged), $custom_pagination[‘pages_text’]);
$pages_text = str_replace(“%TOTAL_PAGES%”, number_format_i18n($max_page), $pages_text);
echo $before.’

‘.$after.”\n”;
}
}
}
?>
[/php]

After adding this above code add the created function into your blog page and then you are done.

Author: Dheeraj Dhawan

I'm a web developer / PHP programmer. I am also working on custom plugin development of wordpress.

Leave a Reply

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