Добавить поле в существующую группу ретрансляторов ACF через код - PullRequest
0 голосов
/ 08 июня 2018

Я использую 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.Можете ли вы, ребята, помочь

Ответы [ 2 ]

0 голосов
/ 08 июня 2018

Попробуйте код ниже, он работает для меня.

    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');
0 голосов
/ 08 июня 2018

Попробуйте что-то вроде этого, это всего лишь пример # как создать поле повторителя, надеюсь, это поможет вам

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',
  )
);
} 
...