每3个foreach添加一个明确的div

时间:2015-02-03 19:16:42

标签: php wordpress foreach

我想在每第3个字词后添加一个明确的div(即<div class="clear></div>),但我在foreach块内遇到这个问题时遇到了困难。但是,我可以在一段时间内循环。

非常感谢任何帮助。

<?php 

$terms = get_field('feature_choices');
// print_r($terms);
if( $terms ): ?>

<?php foreach( $terms as $term ): ?>

    <div class="col-sm-3 margin_bottom">
        <div class="row">
            <div class="col-xs-2">
                <img src="<?php bloginfo('template_directory'); ?>/assets/img/checkmark.png" alt="" />
            </div>
            <div class="col-xs-10">
                <?php echo $term->name; ?>
            </div>
        </div>
    </div>

<?php endforeach; ?>
<?php endif; ?>

1 个答案:

答案 0 :(得分:0)

添加一个计数器

<?php $count = 1; foreach( $terms as $term ): ?>

    <div class="col-sm-3 margin_bottom">
        <div class="row">
            <div class="col-xs-2">
                <img src="<?php bloginfo('template_directory'); ?>/assets/img/checkmark.png" alt="" />
            </div>
            <div class="col-xs-10">
                <?php echo $term->name; ?>
            </div>
        </div>
    </div>
<?php if( $count % 3 == 0 ) echo "\n".'<div class="clear"></div>'; ?>
<?php $count++; endforeach; ?>
<?php endif; ?>