php – 在循环中更改输出最后一项(ACF)

我还在学习PHP,但无法理解这一点.

对于wordpress中的循环,我想输出一个带有地点的列表,用逗号分隔并以一个点结束.

这是我的代码

<?PHP if ( have_rows('subplaats') ): ?>
    <section id="subplaats">
        <div class="subplaats-container">
            <h3 class="support">Wij bestrijden ook in...</h3>
            <p>
                <?PHP while( have_rows('subplaats') ): the_row(); ?>
                    <?PHP $plaats = get_sub_field('plaats'); ?>
                    <?PHP echo $plaats; ?>,<?PHP endwhile; ?>
            </p>
        </div>
    </section>
<?PHP endif; ?>

谁能告诉我怎么做到这一点?我正在使用高级自定义字段.我在层级上做得对吗?

解决方法

您需要计算转发器中的总字段数:

count(get_field('subplaats'));

然后有一个字段计数器来检查当前“计数”字段是否是最后一个字段.

我编辑并测试了你的代码,它运行良好:

<?PHP
      if (have_rows('subplaats')):
              $all_fields_count = count(get_field('subplaats'));
              $fields_count = 1;
?>
<section id="subplaats">
       <div class="subplaats-container">
            <h3 class="support">Wij bestrijden ook in...</h3>
            <p>
            <?PHP
                 while (have_rows('subplaats')): the_row();
                        $plaats = get_sub_field('plaats');
                         echo $plaats;
                        if ($fields_count == $all_fields_count) {
                            echo ".";
                        } else {
                            echo ",";
                        }
                        $fields_count++;
                 endwhile;
            ?>
            </p>
       </div>
</section>
<?PHP
      endif;
?>

干杯!

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...