【问题标题】:Continue or break the while loop继续或中断 while 循环
【发布时间】:2020-06-09 05:27:59
【问题描述】:

我试图弄清楚如何让 while 循环继续在引导列中的另一个部分上发布帖子,或者有没有办法“打破它”,然后让它继续保持原样?第一张图片显示了 while 循环以我想要的方式显示 5 个帖子。下图显示了我希望接下来的 6 个帖子如何保持垂直。现在我让帖子显示,但它从第一个帖子开始,只是复制它们。

  <div class="container">
  <div class="row">
  <?php

  $args = array(
  'post_type' => 'post',
  'posts_per_page' => 5,
   );

   $blogposts = new WP_Query($args);

  $i = 0;
   while($blogposts->have_posts()) {
   $blogposts->the_post();

   if ($i < 2) :
   ?>
   <div class="col-md-6">
   <?php else : ?>
   <div class="col-md-4">
   <?php endif; ?>
  <a href="<?php the_permalink(); ?>">
    <div class="card border-0">
      <div class="card-picture">

        <img class="card-img" src="<?php echo 
      get_the_post_thumbnail_url(get_the_ID()); ?>" alt="Card image">

        <div class="card-img-overlay d-flex flex-column">
          <h5 class="card-title font-weight-bold"><?php the_title(); ?></h5>
          <div class="mt-auto"><?php the_author(); ?> - <i class="fas fa- 
      clock"></i> - <?php the_time('d/m/Y')?></div>
        </div>
      </div>
    </div>
     </a>
   </div>

    <?php
     wp_reset_query();
   $i++;
    }

   ?>



    <div class="container">
      <div class="row">

        <div class="col-md-8">
        <?php
$args = array(
    'post-type' => 'post',
    'posts_per_page' => 6,
);

$blogposts = new WP_Query($args);

while($blogposts->have_posts()) {
    $blogposts->the_post();


     ?>
          <div class="card-3">
            <div class="row no-gutters">
              <div class="col-md-5">
                <a href="<?php the_permalink(); ?>">
                <img class="card-3-img" src="<?php echo 
    get_the_post_thumbnail_url(get_the_ID()); ?>" 
    class="card-img-top h-100" 
    alt="...">
              </div>
              <div class="col-md-7">
                <div class="card-body">
                  <h5 class="card-title-3"><?php the_title(); ?></h5>
                </a>
                  <p class="card-text"><?php the_excerpt(); ?></p>
                  <div class="mt-auto"><?php the_author(); ?> - <i class="fas 
 fa-clock"></i> - <?php the_time('d/m/Y')?></div>
                </div>
              </div>
            </div>
          </div>

          <?php }
wp_reset_query(); ?>

【问题讨论】:

    标签: wordpress twitter-bootstrap while-loop


    【解决方案1】:

    您必须在一个循环中完成所有操作,然后测试 i > 5 是否执行替代功能。重新启动查询后,它将从头开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-11
      • 2015-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多