【发布时间】:2019-02-15 06:54:49
【问题描述】:
我正在尝试连续输出 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 -->
任何帮助将不胜感激,谢谢!
【问题讨论】:
标签: php wordpress while-loop advanced-custom-fields