首页 > 解决方案 > 如何以编程方式在 ACF 中添加自定义字段?

问题描述

我想以编程方式添加一个带有中继器的选项卡,但我似乎找不到解决方案,我搜索了所有可用资源但仍然无法正常工作。

我已经尝试使用acf_add_local_field_groupacf_add_local_field但仍然没有运气。

好吧,我可以使用 acf_add_local_field 创建一个选项卡,但是当我尝试添加一个子项时,在这种情况下它是一个中继器,甚至是一个文本字段,它仍然不起作用。

这是我创建选项卡及其子项的代码,但子项不起作用。

 acf_add_local_field(array(
'key' => 'field_1',
'label' => 'Sub Title',
'name' => 'sub_title',
'type' => '',
'parent' => 'field_5bd14c9349930',
'fields' => array (
    array(
            'key' => 'field_2',
            'label' => 'This is a test',
            'name' => 'my_test',
            'type' => 'text',
            )
        )
));

标签: wordpressadvanced-custom-fields

解决方案


您应该使用acf_add_local_field_group来构造整个字段组。

这是添加组和自定义选项卡的正确代码,其中包含单个转发器字段:

if( function_exists('acf_add_local_field_group') ):

acf_add_local_field_group(array (
    'key' => 'group_1',
    'title' => 'My Group',
    'fields' => array (
        array (
            'key' => 'field_unique_key',
            'label' => 'First Tab', 
            'name' => '',
            'type' => 'tab',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array (
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'placement' => 'top',
            'endpoint' => 0,
        ),
        array (
            'key' => 'field_unique_key',
            'label' => 'Simple Repeater',
            'name' => 'simple_repeater',
            'type' => 'repeater',
            'instructions' => '',
            'required' => 0,
            'conditional_logic' => 0,
            'wrapper' => array (
                'width' => '',
                'class' => '',
                'id' => '',
            ),
            'collapsed' => '',
            'min' => 0,
            'max' => 10,
            'layout' => 'table',
            'button_label' => 'Add row',
            'sub_fields' => array ( // Here you can add as many subfields for this repeater as you want
                array (
                    'key' => 'field_unique_key',
                    'label' => 'Link',
                    'name' => 'link',
                    'type' => 'link', // example link type
                    'instructions' => 'Link name and URL',
                    'required' => 0,
                    'conditional_logic' => 0,
                    'wrapper' => array (
                        'width' => '',
                        'class' => '',
                        'id' => '',
                    ),
                    'return_format' => 'array',
                ),
            ),
        ),
    ),
    'location' => array (
        array (
            array (
                'param' => 'post_type',
                'operator' => '==',
                'value' => 'post',
            ),
        ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'top',
    'instruction_placement' => 'label',
    'hide_on_screen' => '',
));

endif;

推荐阅读