如何在Redux Framework中创建嵌套Repeater(Repeater嵌套Repeater)
问题
我尝试在WordPress主题选项面板中使用Redux Framework创建嵌套Repeater,具体是定义城市列表(分支),并为每个城市添加多个子分支。我使用的代码如下:
Redux::set_section( $opt_name, [ 'title' => 'contact us', 'id' => 'contact-page', 'icon' => 'el el-home', 'fields' => [ [ 'id' => 'branches', 'type' => 'repeater', 'title' => "branches", 'group_values' => true, 'fields' => [ [ 'id' => 'title', 'type' => "text", 'title' => 'city ' ], // inner repeater [ 'id' => 'city-branches', 'type' => 'repeater', 'title' => "branchs inside the city", 'group_values' => true, 'fields' => [ [ 'id' => 'title', 'type' => 'text', 'title' => 'branch name ' ], [ 'id' => 'location', 'type' => 'text', 'title' => "branch lat & long" ] ] ] ] ] ] ] );
但出现错误提示:The repeater field is not supported within the Repeater field. 请问Redux Framework是否支持嵌套Repeater?若不支持,有没有推荐的替代方案或插件?
回答
Redux Framework对嵌套Repeater的支持情况
Redux Framework不支持在Repeater字段内嵌套另一个Repeater字段,这是框架本身的功能限制,你遇到的错误提示已经明确说明了这一点。
替代方案
使用分组字段+多行文本自定义格式
在父Repeater的城市字段中添加一个多行文本字段,要求用户按约定格式输入子分支信息(比如每行一个分支,用|分隔名称和经纬度,格式示例:XX分店|116.40,39.90)。之后在主题代码中通过PHP解析该文本内容,拆分出每个子分支的数据。示例字段配置:
[ 'id' => 'branches', 'type' => 'repeater', 'title' => "branches", 'group_values' => true, 'fields' => [ [ 'id' => 'city_title', 'type' => "text", 'title' => '城市名称' ], [ 'id' => 'city_branches', 'type' => 'textarea', 'title' => "城市子分支", 'desc' => "每行输入一个分支,格式:分支名称|纬度,经度" ] ] ]用单个Repeater字段模拟层级
放弃嵌套结构,直接用单个Repeater字段,每个条目同时包含城市、分支名称、分支经纬度字段。之后在主题代码中通过PHP按城市名称对数据进行分组处理,适合子分支数量不多的场景。自定义字段类型
如果你具备PHP和JavaScript开发能力,可以为Redux Framework自定义一个支持嵌套结构的字段类型,或者寻找第三方开发的Redux扩展字段来实现需求。
替代插件
- Advanced Custom Fields (ACF):原生支持Repeater字段嵌套,能轻松构建城市-子分支的层级结构,主题中调用数据的API简洁易用。
- Meta Box:轻量且功能强大,同样支持嵌套Repeater字段,适合构建复杂的主题选项面板或自定义字段体系。
内容的提问来源于stack exchange,提问作者Ahmad Reza
相关产品推荐
相关产品推荐

