我想弄明白为什么我的分页没有显示出来,我快发疯了。我将设置场景:
我有一个页面“category.php”。在这个页面中,我为所有分类帖子设置了一个自定义查询。它们是自定义帖子类型,并显示该类别的自定义帖子。我的问题如下:
我已经尝试过这个函数:the_post_pagination();
$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\\u分页函数代码
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";
}
}
最合适的回答,由SO网友:Tariq Khan 整理而成
经过一整天的努力,终于找到了解决办法。我在函数中编写这两个函数。最后我得到了预期的结果。
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\');