我还在学习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; ?>
干杯!