我想知道如何在引导列的另一个部分上保持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(); ?>
最合适的回答,由SO网友:Michael 整理而成
“break”是正确的关键字。。。下面是代码结构,如果在标准模板中使用,则无需自定义查询。
<?php
$posts_per_first_section = 6; //how many posts you want to show in the first section//
if ( have_posts() ) :
?>
<!-- opening html for first loop -->
<?php
/* Start the Loop */
while ( have_posts() ) :
the_post();
//your output per post in the first section//
//for example//
get_template_part( \'template-parts/post/content\', get_post_format() );
//check for \'break\' out of first loop//
if( $wp_query->current_post+1 == $posts_per_first_section ) break;
endwhile; ?>
<!-- closing html for first loop -->
<?php //checking if there are more posts to show in second loop
if( $wp_query->current_post > 0 && $wp_query->current_post+1 < $wp_query->found_posts ) : ?>
<!-- opening html for second loop -->
<?php while ( have_posts() ) :
the_post();
//your output per post in the second section//
//for example//
get_template_part( \'template-parts/post/content\', get_post_format() );
endwhile; ?>
<!-- closing html for second loop -->
<?php endif; //end checking if there are more posts to show in second loop
else :
echo \'no posts found\';
endif;
?>