通过代码将字段添加到现有的ACF转发器组

时间:2018-06-08 05:04:10

标签: php wordpress advanced-custom-fields

我正在使用wordpress multisite。我有一个要求,我想在现有的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添加字段。你能帮帮忙吗

2 个答案:

答案 0 :(得分:0)

尝试这样的事情只是#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',
  )
);
} 

答案 1 :(得分:0)

尝试以下代码为我工作。

    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');