【问题标题】:Add a field to a exisiting ACF repeater group via code通过代码将字段添加到现有 ACF 转发器组
【发布时间】:2018-11-18 02:28:50
【问题描述】:

我正在使用 wordpress 多站点。我有一个要求,我想向现有的 ACF 中继器组添加一个字段。以下是我尝试过的。

add_action('acf/init','add_field');
function add_field(){

acf_add_local_field(array(
            'key' => 'field_asdfaas1234ww',
                    'label' => 'price_xyz',
                    'name' => 'price_xyz',
                    'type' => 'text',
                    'instructions' => '',
                    'parent' => 'field_568a6ed7971d2',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => '',
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'formatting' => 'html',
                    'maxlength' => '',));   


}

以下是现有 ACF 组的导出代码,仅供参考..

if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array (
    'key' => 'group_59ce208402798',
    'title' => 'price_group',
    'fields' => array (
        array (
            'key' => 'field_568a6ed7971d2',
            'label' => 'pricing',
            'name' => 'pricing',
            'type' => 'repeater',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array (
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'row_min' => '',
            'row_limit' => '',
            'layout' => 'table',
            'button_label' => 'Add Row',
            'min' => 0,
            'max' => 0,
            'collapsed' => '',
            'sub_fields' => array (
                array (
                    'key' => 'field_568a6f4f971d3',
                    'label' => 'group_name',
                    'name' => 'group_name',
                    'type' => 'taxonomy',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'taxonomy' => 'product_cat',
                    'field_type' => 'select',
                    'allow_null' => 0,
                    'load_save_terms' => 0,
                    'return_format' => 'id',
                    'multiple' => 0,
                    'add_term' => 1,
                    'load_terms' => 0,
                    'save_terms' => 0,
                ),
                array (
                    'key' => 'field_568a6f65971d4',
                    'label' => 'width',
                    'name' => 'width',
                    'type' => 'text',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => '',
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'formatting' => 'html',
                    'maxlength' => '',
                ),
                array (
                    'key' => 'field_568a72a5ffda9',
                    'label' => 'no_of_doors',
                    'name' => 'no_of_doors',
                    'type' => 'text',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => '',
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'formatting' => 'html',
                    'maxlength' => '',
                ),
                array (
                    'key' => 'field_568a72baffdaa',
                    'label' => 'price_norway',
                    'name' => 'price_norway',
                    'type' => 'text',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => '',
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'formatting' => 'html',
                    'maxlength' => '',
                ),
                array (
                    'key' => 'field_568b5cae9ff93',
                    'label' => 'price_swedish',
                    'name' => 'price_swedish',
                    'type' => 'text',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => '',
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'formatting' => 'html',
                    'maxlength' => '',
                ),
            ),
        ),
    ),
    'location' => array (
        array (
            array (
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'product',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'seamless',
    'label_placement' => 'top',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
    'active' => 1,
    'description' => '',
));

endif;

但由于某种原因,我无法通过我的代码 function add_field 添加字段。各位大神能帮忙吗

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    尝试这样的事情,它只是#how 创建中继器字段的一个示例, 希望对你有帮助

    add_action( 'acf/init', 'acf_add_field' );
    function acf_add_field() {
        /**
         * Repeater field
         */
        acf_add_local_field(
      array (
        'key' => 'field_56ff5b1a77c79',
        'label' => 'Repeatable image with optional link',
        'name' => 'repeatable_imgs',
        'type' => 'repeater',
        'parent' => 'field_56ff5b1a77c75', //flex field key
        'parent_layout' => '56ff5b1a77c76', // layout key
        'row_min' => 0,
        'row_limit' => '',
        'layout' => 'table',
        'button_label' => 'Add image',
      )
    );
        acf_add_local_field(
      array (
      'key' => 'field_5237950f6c6e4',
      'label' => 'url',
      'name' => 'img_link_url',
      'type' => 'text',
      'parent' => 'field_56ff5b1a77c79',
      )
    );
    } 
    

    【讨论】:

    • 我不想添加组,我只想将字段添加到我使用 acf_add_local_field 的现有组中
    【解决方案2】:

    试试下面的代码,它对我有用。

        function my_acf_add_local_field_groups() {
    
    
    acf_add_local_field(array(
    'key' => 'pricing',
    'label' => 'Pricing',
    'name' => 'pricing',
    'type' => 'repeater',
    'parent' => 'group_5b1a4899b1ab3',
             'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array (
                'width' => '',
                'class' => '',
                'id' => '',
            ),'row_min' => '',
            'row_limit' => '',
            'layout' => 'table',
            'button_label' => 'Add Row',
            'min' => 0,
            'max' => 0,
      'collapsed' => '','sub_fields' => array (
                array (
                    'key' => 'field_568a6f4f971d3',
                    'label' => 'group_name',
                    'name' => 'group_name',
                    'type' => 'taxonomy',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'taxonomy' => 'product_cat',
                    'field_type' => 'select',
                    'allow_null' => 0,
                    'load_save_terms' => 0,
                    'return_format' => 'id',
                    'multiple' => 0,
                    'add_term' => 1,
                    'load_terms' => 0,
                    'save_terms' => 0,
                ),
                array (
                    'key' => 'field_568a6f65971d4',
                    'label' => 'width',
                    'name' => 'width',
                    'type' => 'text',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => '',
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'formatting' => 'html',
                    'maxlength' => '',
                ),
                array (
                    'key' => 'field_568a72a5ffda9',
                    'label' => 'no_of_doors',
                    'name' => 'no_of_doors',
                    'type' => 'text',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => '',
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'formatting' => 'html',
                    'maxlength' => '',
                ),
                array (
                    'key' => 'field_568a72baffdaa',
                    'label' => 'price_norway',
                    'name' => 'price_norway',
                    'type' => 'text',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => '',
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'formatting' => 'html',
                    'maxlength' => '',
                ),
                array (
                    'key' => 'field_568b5cae9ff93',
                    'label' => 'price_swedish',
                    'name' => 'price_swedish',
                    'type' => 'text',
                    'instructions' => '',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'default_value' => '',
                    'placeholder' => '',
                    'prepend' => '',
                    'append' => '',
                    'formatting' => 'html',
                    'maxlength' => '',
                ),
            ),
    
         ));
       }
    
    
     add_action('acf/init', 'my_acf_add_local_field_groups');
    

    【讨论】:

    • 我不想添加组,我只想将字段添加到我使用acf_add_local_field的现有组中
    • 好的,我已经添加了一个组并使用 acf_add_local_field 添加字段让我编辑答案
    猜你喜欢
    • 2014-09-12
    • 2021-04-26
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 2021-03-01
    • 2018-06-18
    • 1970-01-01
    • 2018-10-06
    相关资源
    最近更新 更多