My custom pagination function returns empty pages at the end. It doesn't check when the loop ends, and does not stop counting pages - blank pages at the end - counting empty result? Ive try to add optional parameters and other things, but it still not working how i want.
function custom_pagination($numpages = '', $pagerange = '2', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
if ( is_home() ) {
$format = '?paged=%#%';
} else if ( !get_option('permalink_structure') ) {
$format = '&paged=%#%';
} else {
$format = 'page/%#%/';
}
$pagination_args = array(
'base' => @add_query_arg('paged','%#%'),
'format' => '',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
echo "<span class='page-numbers page-num'><a class='showing_pages'>Showing Page " . $paged . " of " . $numpages . "</a></span> <span class='page_numbers'>";
echo $paginate_links;
echo "</span></nav>";
}
}
Here i call this function:
$quert = array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' =>'subject',
'value' => $_GET['subject'],
'compare' => 'LIKE',
),
array(
'key' => 'job_type',
'value' => $_GET['job_type'],
'compare' => 'LIKE',
),
array(
'key' => 'job_title',
'value' => $_GET['job_title'],
'compare' => 'LIKE'
),
),
array(
'key' => 'location',
'value' => $_GET['location'],
'compare' => 'LIKE',
)
);
/*$startdate = get_field("start_date");
$startdate = get_field("start_date");*/
/* $number_of_jobs_on_each_page= 12;*/
$number_of_jobs_on_each_page=get_field("number_of_jobs_on_each_page");
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
/*$date = date_create($stdate);
echo date_format($date, 'Ymd');*/
$custom_args = array(
'post_type' => array( 'job' ),
'order' => 'DESC',
'orderby' => 'post_date',
'posts_per_page' => $number_of_jobs_on_each_page,
'paged' => $paged,
/* 'meta_key' => 'start_date',
'meta_type' => 'DATE',
'orderby' => 'meta_value_num',
'order' => 'ASC',*/
'meta_query' => $quert,
);
$custom_query = new WP_Query( $custom_args );
if (function_exists(custom_pagination)) {
custom_pagination($custom_query->max_num_pages,"",$paged);
}