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 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line