You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Elementor Repeater字段标题动态设置为选择控件选中项的显示标签

解决方案

你可以通过Elementor编辑器端的JS过滤器实现读取SELECT控件选中项的label值显示,步骤如下:

  • 正常注册Repeater及内部的SELECT控件,示例代码:
$repeater->add_control(
    'custom_select',
    [
        'label' => '测试选择项',
        'type' => \Elementor\Controls_Manager::SELECT,
        'options' => [
            'val_1' => '正式环境',
            'val_2' => '测试环境',
            'val_3' => '灰度环境',
        ],
        'default' => 'val_1',
    ]
);
  • 在Elementor编辑器加载的自定义JS脚本中,注册过滤器处理Repeater标题的渲染逻辑:
elementor.hooks.addFilter( 'elementor/controls/repeater/print_item_title', function( title, item ) {
    // 替换为你自己的SELECT控件字段名
    const targetSelect = 'custom_select';
    // 和PHP中定义的SELECT选项保持完全一致
    const selectLabelMap = {
        'val_1': '正式环境',
        'val_2': '测试环境',
        'val_3': '灰度环境',
    };

    const selectedValue = item.model.get(targetSelect);
    if (selectedValue && selectLabelMap[selectedValue]) {
        title = title.replace(`{{{${targetSelect}}}}`, selectLabelMap[selectedValue]);
    }
    return title;
} );
  • 配置Repeater的title_field参数即可正常使用该字段:
$this->add_control(
    'list',
    [
        'label' => '列表项',
        'type' => \Elementor\Controls_Manager::REPEATER,
        'fields' => $repeater->get_controls(),
        'title_field' => '环境类型:{{{ custom_select }}}',
    ]
);

可选优化:如果选项配置频繁改动,可以通过PHP将SELECT的options数组通过wp_localize_script注入到JS全局变量中,避免PHP和JS两端重复配置不一致的问题。

内容的提问来源于stack exchange,提问作者Simon Josef Kok

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 23:45:03