I’m going nuts trying to figure out why my pagination isn’t showing up. I’ll set the scene:
I have a page "category.php". In this page I have a custom query set up for all of my categorize posts. They are custom post types and showing custom post of that category. My query is as follows:
I am already tried this function: the_post_pagination();
global $wp_query;
$category = get_category(get_query_var('cat'));
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
$cat = new WP_Query(
array(
'post_type' => 'shield',
'category_name' => $category->name,
'posts_per_page' => 10,
'paged' => $paged
));
if ( $cat->have_posts() ) {
while ( $cat->have_posts() ) {
$cat->the_post();
}
mom_pagination($cat->max_num_pages);
wp_reset_query();
}
mom_pagination function code goes here:
function mom_pagination($pages = '', $range = 10)
{
global $wp_query;
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); }
elseif ( get_query_var('page') ) { $paged = get_query_var('page'); }
else { $paged = 1; }
if (mom_option('pagi_type') == false) {
$showitems = ($range * 2)+1;
if(empty($paged)) $paged = 1;
if($pages == '' && $pages != 0)
{
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages)
{
echo "<div class='pagination'>";
if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>";
for ($i=1; $i <= $pages; $i++)
{
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems ))
{
echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>";
}
}
if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>›</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>";
echo "</div>\n";
}
} else {
$big = 999999999; // need an unlikely integer
echo "<div class='pagination'>";
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, $paged ),
'total' => $pages
) );
echo "</div>\n";
}
}
i expect that it should display the pagination.
After an exhausted effort of a whole day finally find the solution. i write these two functions in my functions.php file and finally i got the expected result.
function remove_page_from_query_string($query_string)
{
if ($query_string['name'] == 'page' && isset($query_string['page'])) {
unset($query_string['name']);
// 'page' in the query_string looks like '/2', so i'm spliting it out
list($delim, $page_index) = split('/', $query_string['page']);
$query_string['paged'] = $page_index;
}
return $query_string;
}
// I will kill you if you remove this. I died two days for this line
add_filter('request', 'remove_page_from_query_string');
// following are code adapted from Custom Post Type Category Pagination Fix by jdantzer
function fix_category_pagination($qs){
if(isset($qs['category_name']) && isset($qs['paged'])){
$qs['post_type'] = get_post_types($args = array(
'public' => true,
'_builtin' => false
));
array_push($qs['post_type'],'post');
}
return $qs;
}
add_filter('request', 'fix_category_pagination');