【问题标题】:Wordpress Pagination Only showing 2 pagessWordpress 分页只显示 2 页
【发布时间】:2019-01-22 02:57:59
【问题描述】:

我设置了一个基本主页,每页显示 3 篇文章,并通过分页浏览这些页面。目前它只会显示第 1 页和第 2 页的分页,即使我有 12 篇文章导致 4 页。我不太确定我哪里出错了:

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

    $args = array(
        'post_type'         => 'post',
        'post_status'       => 'publish',
        'paged'             => $paged,
        'posts_per_page'    => 2
    );
    $the_query = new WP_Query( $args );
    while ($the_query -> have_posts()) : $the_query -> the_post();

        include(locate_template('content-post.php' ));

    endwhile;
?>

<?php the_posts_pagination( array('mid_size' => 3) ); ?>

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:

    the_posts_pagination 使用默认的 WP 查询,所以它在这里不起作用。你能试试下面的代码吗:

    <?php
        if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }
    
        $args = array(
            'post_type'         => 'post',
            'post_status'       => 'publish',
            'paged'             => $paged,
            'posts_per_page'    => 2
        );
        $temp = $wp_query;
        $wp_query= null;
        $wp_query = new WP_Query($args);
        while ($wp_query -> have_posts()) : $wp_query -> the_post();
            include(locate_template('content-post.php' ));
        endwhile;
    
    
        the_posts_pagination( array('mid_size' => 3) );
    
        $wp_query = null;
        $wp_query = $temp;
        wp_reset_query();
    ?>
    

    代码经过完美测试。

    【讨论】:

    • 这样可以使分页链接的相关数量正常工作,但是超过 2 的任何页面都直接进入 404。例如/page/2/ 工作正常,但 /pages/3/ 产生 404。
    • 谢谢,但还是一样,分页适用于第 1-2 页,然后是第 3 页,从 404 开始。
    • 你在哪个页面使用上面的代码?在这里它工作得很好
    • 所以我用在home.php上
    【解决方案2】:

    您的代码在我看来没问题。我遇到了同样的问题,我通过将posts_per_page 与位于管理仪表板中Settings-&gt;Reading 中的Blog pages show at most 字段匹配来解决它

    示例:

    "posts_per_page" =&gt; 6 那么Blog pages show at most 也应该是 6

    我希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      使用这个插件Click here

      并使用此简码进行分页&lt;?php wp_pagenavi(); ?&gt;

      我为我的项目及其工作使用了精确的以下循环。

      <?php
      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $args= array(
       'cat' => '',
       'orderby'=> 'date',
       'order'=> 'DESC',
       'paged' => $paged ,
       'posts_per_page' => 3
      );
      query_posts($args); 
      if (have_posts()) :
        while (have_posts()):
          the_post();
        endwhile;
      endif;
      ?>
      

      请试一试。希望它也对你有用。

      如果你在主页上显示你的帖子,你需要替换

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

      <?php
      
      if ( get_query_var('paged') ) 
          { 
              $paged = get_query_var('paged'); 
          } 
      else if ( get_query_var('page') ) 
          {
              $paged = get_query_var('page'); 
          }
          else 
          {
              $paged = 1; 
          }
      

      【讨论】:

      • 这很好,但它仍然只在分页中显示 2 页。
      【解决方案4】:

      问题是 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;检索主查询对象的数据,但不检索自定义对象的数据。 我在这里找到了解决方案https://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多