【问题标题】:Loop with pagination only showing two pages分页循环仅显示两页
【发布时间】:2020-01-29 21:41:45
【问题描述】:

我是 wordpress 的最后一名大三学生,想用 wp_query 创建**pagination**,但我只有 2 页,但我有 8 个帖子,每页显示 2 个 --我在不同的网站上搜索了很多,但没有找到答案

<?php //GET POSTS
            $paged = get_query_var( 'paged' );
            $arr=array(
             'author'         =>the_author_meta('id'),
             'posts_per_page' => 2,
             'paged'          =>$paged,
            );

            $special_query= new WP_Query($arr);//SPECIAL QUERY

            if($special_query->have_posts()){//check if there is posts or no
                while($special_query->have_posts()){
                    $special_query-> the_post();
            /*content*/
            ?>

pagination

【问题讨论】:

    标签: php wordpress pagination


    【解决方案1】:

    试试这个

    $query = array(
        'post_type' => 'post',
        'posts_per_page' => 2,
        'author'=>the_author_meta('id'),
        'ignore_sticky_posts' => true,
        'paged' => get_query_var('paged') ? get_query_var('paged') : 1 
    );
    $loop = new WP_Query($query);
    
    if( $loop->have_posts() ):
        while ( $loop->have_posts() ) : $loop->the_post();
           //postcode
        endwhile;
    endif;
    
    $big = 9999; // need an unlikely integer
        echo paginate_links( array(
            'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
            'format' => '?paged=%#%',
            'current' => max(1, get_query_var('paged') ),
            'total' => $loop->max_num_pages
    ) );
    

    【讨论】:

      【解决方案2】:

      您正在制作模板吗?试试这个代码,让我知道它是否有效

      <?php 
      
      $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
      
      $args = array(
          'posts_per_page' => 5,
          'author' =>the_author_meta('id'),
          'paged' => $paged,
      );
      
      $special_query = new WP_Query( $args );
      
      if($special_query->have_posts()){
          while($special_query->have_posts()){
              $special_query-> the_post();
                  echo get_the_title() . "<br>";
          }
      }
      $big = 999999999; // need an unlikely integer
      
      echo paginate_links( array(
          'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
          'format' => '?paged=%#%',
          'current' => max( 1, get_query_var('paged') ),
          'total' => $special_query->max_num_pages
      ) );
      

      【讨论】:

      • 您是否保存了带有帖子名称的永久链接?
      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-01
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多