如何为Advanced Custom Fields Repeater的所有重复字段添加外层容器
解决ACF Repeater字段整体包裹容器的问题
你的问题出在闭合div的位置错误——原来的代码把</div>放在了while循环内部,导致每一条重复项都被单独包裹成一个#multiple容器,而不是整个Repeater字段被统一包裹。
修正后的代码如下:
<?php //begin some repeating fields for if multiple trips to same country elseif (get_row_layout() == 'trip_card') : ?> <div id="multiple"> <?php if (have_rows('multiple_trips')) : // loop through the rows of data while (have_rows('multiple_trips')) : the_row(); //vars $first_trip_title = get_sub_field('first_trip_title'); $first_trip_date = get_sub_field('first_trip_date'); //display a sub field value ?> <p><strong><?php echo $first_trip_title; ?>:</strong></p> <p><?php echo $first_trip_date; ?></p> <?php endwhile; // 结束while循环 ?> </div><!-- end of multiple --> <?php endif; // 结束have_rows判断 ?>
关键修正点:
- 将
</div><!-- end of multiple -->移到while循环的endwhile之后,确保整个Repeater的所有重复项都被包含在同一个#multiple容器内 - 补全
endwhile和endif语句,保证PHP条件判断的语法完整性(原代码缺少这两个语句,会导致语法错误)
这样修改后,整个multiple_trips Repeater字段的所有重复内容都会被包裹在id为multiple的div中,你就可以通过Grid布局来统一样式化这些重复元素了。
内容的提问来源于stack exchange,提问作者fourwhitesocks
相关产品推荐
相关产品推荐

