WordPress分页功能仅在首页上发布

时间:2018-07-13 05:22:16

标签: php html wordpress bootstrap-4

我正在尝试创建一个帖子,第一页有一个特色帖子,然后是三列帖子。

首页

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

然后第二页就是三列的行

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

我有以下代码设置,此刻似乎无法区分结果的第一页和第二页的结果,因为它似乎将第二页上的计数器重新计算为0。

  <?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>      

您知道为什么计数器在结果的第二页上重置为0吗?我相信这可以解决我的问题并显示所需的布局。

1 个答案:

答案 0 :(得分:1)

(请阅读// {comment here},然后将此代码与您的代码进行比较,看看有什么变化。)

  <?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>