doudan1123 2018-07-13 05:22
浏览 54
已采纳

仅限首页上的WordPress分页功能帖子

I'm attempting to create a posts with the first page having a feature post followed by rows of 3 columns of posts.

First page

|---------------Feature Post---------------|
|----Post----| |----Post----| |----Post----|
|----Post----| |----Post----| |----Post----|

Then for the second page to just be rows of 3 columns

|----Post----| |----Post----| |----Post----|
|----Post----| |----Post----| |----Post----|

I have the following code setup which at the moment doesn't seem to differentiate between the first page of results and the second page of results as it seems to recalculate the counter back to 0 on the second page.

  <?php 

      $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
      $args = array(
         'post_type' => 'post',
         'post_status' => 'publish',
         'posts_per_page' => 4,
         'paged' => $paged,
         'prev_next' => true,
        );

      $wp_query = new WP_Query($args);          

      if( $wp_query->have_posts() ) {

        $counter = 0;

        while ($wp_query->have_posts()) {
          $wp_query->the_post();
          echo $counter
        ?>

        <?php if ( 0 === $counter ) { ?> 

         <div class="row">
            <div class="col-12">

            ======First article here=====

            </div>
         </div>

     <?php } else { ?>

     <?php if($counter % 3 === 0) { ?> 
          <div class="row">
        <?php } ?>
       <div class="col-md-4 news-grid__article">

         ===== All other articles here =====

       </div>

    <?php $counter++; 
      if($counter != 0 && $counter % 3 == 0) { ?>
        </div>
        <div class="clearfix"></div>
      <?php } ?>


  <?php } $counter++; ?>


  <?php } } ?>

  <div class="news-pagination">
      <?php 
          echo paginate_links( array(

              'prev_next'    => true

          ) );
      ?>
  </div>      

Any idea why the counter resets to 0 on the second page of results? I believe this would solve my issue and show the layout required.

  • 写回答

1条回答 默认 最新

  • douqiao3930 2018-07-14 04:24
    关注

    (Please read the // {comment here} and just compare this code with yours to see what changed.)

      <?php 
    
          $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
          $args = array(
             'post_type' => 'post',
             'post_status' => 'publish',
             'posts_per_page' => 10,
             'paged' => $paged,
             //'prev_next' => true,
            );
    
          // Note that I'm not overriding the global `$wp_query`.
          $my_query = new WP_Query($args);          
    
          if( $my_query->have_posts() ) {
    
            $counter = 0;
            $is_paged = $my_query->is_paged;
            $new_row = true;
    
            while ($my_query->have_posts()) {
              $my_query->the_post();
              //echo $counter
            ?>
    
            <?php if ( ! $is_paged && 0 === $counter ) { ?> 
    
             <div class="row">
                <div class="col-12">
    
                ====== Post #<?php echo $counter + 1; ?>; ID: <?php the_ID(); ?> =====
                <h3><?php the_title(); ?></h3>
    
                </div>
             </div>
    
         <?php } else { ?>
    
         <?php if($new_row) { ?> 
              <div class="row">
            <?php } ?>
           <div class="col-md-4 news-grid__article">
    
             ===== Post #<?php echo $counter + 1; ?>; ID: <?php the_ID(); ?> =====
             <h4><?php the_title(); ?></h4>
    
           </div>
    
        <?php //$counter++; 
          if(
            ( ! $is_paged && $counter % 3 === 0 ) ||   // every 3rd post on page #1
            ( $is_paged && $counter % 3 === 2 )   ||   // every 3rd post on page #2, #3, etc.
            ( $counter === $my_query->post_count - 1 ) // the last post
          ) { ?>
            </div>
            <div class="clearfix"></div>
          <?php $new_row = true;
          } else {
              $new_row = false;
          } ?>
    
    
      <?php } $counter++; ?>
    
    
      <?php } } ?>
    
      <div class="news-pagination">
          <?php 
              echo paginate_links( array(
    
                  'prev_next' => true,
                  // Here's how you can make `paginate_links()` works without having
                  // to override the `WP_Query` global instance (i.e. `$wp_query`).
                  'total'     => $my_query->max_num_pages,
                  'current'   => $my_query->get( 'paged' ),
    
              ) );
          ?>
      </div> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教