PHP将列表项拆分为2列

时间:2014-08-11 16:58:05

标签: php wordpress advanced-custom-fields

我正在运行一个Wordpress网站,该网站使用单独的ACF字段来存储项目符号文本。

<?php if( get_field('bullet_points') ): ?>
                <div class="row">
                    <div class="col-3">
                        <p class="big"><?php the_field('bullet_point_text'); ?></p>
                    </div>

                    <div class="col-3">
                        <ul>
                            <?php while( has_sub_field('bullet_points') ): ?>
                            <?php $post_counter++; ?>
                            <li><?php the_sub_field('individual_bullet_point'); ?></li>
                                <?php
                                    if ( 0 == $post_counter % 5 ) {
                                    echo '</ul></div><div class="col-3"><ul>';
                                    }
                                ?>
                            <?php endwhile; ?>
                        </ul>
                    </div>
                </div>
                <?php endif; ?>

上面的代码采用字段(bullet_points)并每5个点在多列中输出它们 - 我对其进行了硬编码以确保代码正常工作。如何将它们输出到2个(均匀间隔的)列表中?

所以,如果我有6分,我想在第一个DIV中获得3分,在第二个DIV中需要3分。同样,如果我有7分,我想在第一个DIV中有3个,在第二个DIV中需要4个。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

我相信这会奏效......

<?php $count_bullets = count(get_sub_field('bullet_points')); ?>
<?php $col_items = ceil( $count_bullets / 2 ); ?>
<?php while( has_sub_field('bullet_points') ): ?>
<?php $post_counter++; ?>
<li><?php the_sub_field('individual_bullet_point'); ?></li>
    <?php
        if ( 0 == $post_counter % $col_items ) {
        echo '</ul></div><div class="col-3"><ul>';
        }
    ?>
<?php endwhile; ?>
相关问题