首页 视频课程 WordPress开发文档文章循环输出与重置 结构控制

结构控制

2020-04-03 / 2642阅

本视频需要登录以后才能播放

登录

在我们日常开发过程中,我们会有这样的需求:

第一篇文章需要输出摘要或者缩略图,以后的文章不需要。

那我们可以通过两次循环,第二次进行偏移来实现。代码如下:

<?php
       $args = array(
              'post_type' =>'post',
              'showposts'=>1
       );
       $query = new WP_Query( $args );
       while ($query->have_posts()):$query->the_post();
              ?>
              <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
              <br />
              <?php the_content();?>
              <br />
       <?php
       endwhile;
       $args = array(
              'post_type' =>'post',
              'offset'=>1
       );
       $query = new WP_Query( $args );
       while ($query->have_posts()):$query->the_post();
              ?>
              <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
              <br />
       <?php
       endwhile;
?> 

以上可以实现我们的想法,那么如何一次循环就实现呢?

<?php
       $args = array(
              'post_type' =>'post',
       );
       $query = new WP_Query( $args );
       $i=1;
       while ($query->have_posts()):$query->the_post();
              ?>
              <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
              <br />
              <?php
                     if($i%3 == 0){  //$i/2如果能够整除就是0    不是整除就有余数
                            the_content();
                            echo '<br />';
                     }
              ?>

       <?php
       $i++;
       endwhile;
?> 

阅读文章或者观看视频过程中有任何问题,请下方留言或者联系我Q248758228

大家谈论
    我的见解
    目录