pagination leads to 404 page

时间:2019-07-17 作者:Tariq Khan

我想弄明白为什么我的分页没有显示出来,我快发疯了。我将设置场景:

我有一个页面“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)."\'>&laquo;</a>";
         if($paged > 1 && $showitems < $pages) echo "<a href=\'".get_pagenum_link($paged - 1)."\'>&lsaquo;</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)."\'>&rsaquo;</a>";  
         if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<a href=\'".get_pagenum_link($pages)."\'>&raquo;</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";
     }

}

1 个回复
最合适的回答,由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\');

相关推荐

在Get_the_Posts_Pagination函数中编辑分页文本

我想在链接模板中编辑screen\\u reader\\u文本。php我可以在一个主题中这样做,这样它就不会在更新时被覆盖。看起来过滤器是最好的选择,但我找不到关于使用什么过滤器的文档。这是我想从链接模板更改的代码。php: if ( $GLOBALS[\'wp_query\']->max_num_pages > 1 ) { $args = wp_parse_args( $args, array( \'mid_size\' =&