WordPress分页功能不起作用(什么都不显示)

时间:2019-07-17 08:00:37

标签: wordpress pagination

我很想弄清楚为什么我的分页没有显示出来。我将设置场景:

我有一个页面“ category.php”。在此页面中,我为所有分类帖子设置了自定义查询。它们是自定义帖子类型,并显示该类别的自定义帖子。我的查询如下:

我已经尝试过此功能: 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 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";
     }

}

我希望它应该显示分页。

2 个答案:

答案 0 :(得分:1)

经过一整天的努力,终于找到了解决方案。 我在我的functions.php文件中编写了这两个函数,最后我得到了预期的结果。

 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');

答案 1 :(得分:0)

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

$category = get_category(get_query_var('cat')); 

$args = array(
'post_type'   => 'shield',
'category_name' => $category->name,
'post_status' => 'publish',
'posts_per_page' => 10,
'paged' => $paged);

$cat = new WP_Query( $args );
if( $cat->have_posts() ) :

  while( $cat->have_posts() ) :
      $cat->the_post();
  endwhile;

  wp_reset_postdata();

endif;


?>

<div>
    <div class="NewsPrev">
        <?php previous_posts_link('< Pref',$cat->max_num_pages)?>
    </div>
    <div class="NewsNext">
        <?php next_posts_link('Next >',$cat->max_num_pages)?>
    </div>
</div>

如果您收到404,请尝试以下操作:

  1. 刷新永久链接:Wordpress / settings / permalinks =>保存更改
  2. 永久链接结构:Wordpress / settings / permalinks =>设置为“帖子名称”
  3. wordpress / settings / reading / =>将'options-reading'WordPress参数设置为与'posts_per_page'=> 10相同(不确定 cpt 是否需要)