我正在尝试连续输出3个帖子,如果超过3个,则移至下一行。如果帖子的数量不是3的倍数,则需要关闭该行。此刻,此代码下的内容正在中断,具体取决于有多少帖子(该行没有得到应有的关闭)。
<div class="choose-style">
<h2>Choose a style</h2>
<?php
$f = 1;
//Start loop
$loop = new WP_Query( array( 'post_type' => 'styles', 'orderby' => 'date', 'order' => 'ASC', 'posts_per_page' => '6' ) );
if ( $loop->have_posts() ) : ?>
<!-- Wrapper for slides -->
<div class="image-grid-wrapper">
<?php while ( $loop->have_posts() ) : $loop->the_post();
if ($f == 1) { ?>
<div class="row">
<?php } ?>
<div class="col-sm-4">
<div class="image-grid-item">
<?php
$carousel = get_field('styles_slider');
$first = $carousel[0]['image'];
if ($first != NULL) {
$image = $first['sizes']['medium'];
} else {
$image = get_template_directory_uri() . "/assets/img/placeholder.png";
}
?>
<div class="image-wrapper">
<a href="<?php the_permalink(); ?>">
<div class="image" style="background-image: url('<?php echo $image; ?>')">
<div class="overlay">
<div class="overlay-inner">
<p><?php the_title(); ?></p>
</div>
</div>
</div>
</a>
</div>
</div>
</div>
<?php if ($f == 3 ) { ?>
</div>
<?php $f = 1; continue; };
$f++; ?>
<?php endwhile; endif;
if($f < 3) { ?>
</div><!--END OF POSTS -->
<?php }; wp_reset_postdata(); ?>
</div><!-- end image grid wrapper -->
<div class="row">
<div class="col-md-8 col-md-offset-2">
<?php the_field('choose_style_text'); ?>
</div>
</div>
</div><!-- end choose-style -->
任何帮助将不胜感激,谢谢!
答案 0 :(得分:0)
如何...更改
if ($f == 1) { ?>
<div class="row">
<?php } ?>
到(如果$ f为1或4、7、10等开始该行)
if ($f == 1 || ($f-1)%3 == 0) { ?>
<div class="row">
<?php } ?>
并更改
<?php if ($f == 3 ) { ?>
</div>
<?php $f = 1; continue; };
到(如果$ f被3整除,则关闭该行)
if ($f%3 === 0) { ?>
</div>
<?php } ?>