Wordpress ACF以编程方式向灵活的内容添加行

时间:2019-03-28 06:56:53

标签: wordpress advanced-custom-fields acfpro

我想在我的帖子灵活内容字段中添加一行。 我使用导出工具中的代码创建ACF字段,这是其中的一部分:

acf_add_local_field_group(array(
        'key' => 'group_5c4c39a08349a',
        'title' => 'Posts page',
        'fields' => array(
            array(
                'key' => 'field_5c4c39aab1ed4',
                'label' => 'Blokken positie',
                'name' => 'block_position',
                'type' => 'radio',
                'instructions' => 'Geef aan waar de pagina blokken geplaatst moeten worden',
                'required' => 1,
                'conditional_logic' => 0,
                'wrapper' => array(
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'choices' => array(
                    0 => 'Boven de berichten',
                    1 => 'Onder de berichten',
                ),
                'allow_null' => 0,
                'other_choice' => 0,
                'default_value' => 1,
                'layout' => 'vertical',
                'return_format' => 'value',
                'save_other_choice' => 0,
            ),
        ),
        'location' => array(
            array(
                array(
                    'param' => 'page_type',
                    'operator' => '==',
                    'value' => 'posts_page',
                ),
            ),
        ),
        'menu_order' => 6,
        'position' => 'normal',
        'style' => 'default',
        'label_placement' => 'top',
        'instruction_placement' => 'label',
        'hide_on_screen' => '',
        'active' => true,
        'description' => '',
    ));

    acf_add_local_field_group(array(
        'key' => 'group_5c4a061a6be68',
        'title' => 'Page/ Post blocks',
        'fields' => array(
            array(
                'key' => 'field_5c4a0645c3baa',
                'label' => 'Blocks',
                'name' => 'blocks',
                'type' => 'flexible_content',
                'instructions' => '',
                'required' => 0,
                'conditional_logic' => 0,
                'wrapper' => array(
                    'width' => '',
                    'class' => '',
                    'id' => '',
                ),
                'layouts' => array(
                    'layout_5c4a084c7953b' => array(
                        'key' => 'layout_5c4a084c7953b',
                        'name' => 'news_slider',
                        'label' => 'Nieuws slider',
                        'display' => 'block',
                        'sub_fields' => array(
                            array(
                                'key' => 'field_5c4a085f7953e',
                                'label' => 'Type',
                                'name' => 'type',
                                'type' => 'select',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'choices' => array(
                                    0 => 'Laatste berichten',
                                    1 => 'Berichten uit een categorie',
                                    2 => 'Zelf de berichten selecteren',
                                ),
                                'default_value' => array(
                                ),
                                'allow_null' => 0,
                                'multiple' => 0,
                                'ui' => 0,
                                'return_format' => 'value',
                                'ajax' => 0,
                                'placeholder' => '',
                            ),
                            array(
                                'key' => 'field_5c4ac2d1986b9',
                                'label' => 'Aantal nieuws artikelen',
                                'name' => 'news_items',
                                'type' => 'number',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => array(
                                    array(
                                        array(
                                            'field' => 'field_5c4a085f7953e',
                                            'operator' => '==',
                                            'value' => '0',
                                        ),
                                    ),
                                    array(
                                        array(
                                            'field' => 'field_5c4a085f7953e',
                                            'operator' => '==',
                                            'value' => '1',
                                        ),
                                    ),
                                ),
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'default_value' => 9,
                                'placeholder' => '',
                                'prepend' => '',
                                'append' => '',
                                'min' => 3,
                                'max' => 18,
                                'step' => 3,
                            ),
                            array(
                                'key' => 'field_5c4a093b2ba54',
                                'label' => 'Toon nieuws uit categorie',
                                'name' => 'news_category',
                                'type' => 'taxonomy',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => array(
                                    array(
                                        array(
                                            'field' => 'field_5c4a085f7953e',
                                            'operator' => '==',
                                            'value' => '1',
                                        ),
                                    ),
                                ),
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'taxonomy' => 'category',
                                'field_type' => 'select',
                                'allow_null' => 0,
                                'add_term' => 1,
                                'save_terms' => 0,
                                'load_terms' => 0,
                                'return_format' => 'object',
                                'multiple' => 0,
                            ),
                            array(
                                'key' => 'field_5c4a09e7ee82f',
                                'label' => 'Selecteer nieuws berichten',
                                'name' => 'news_item_collection',
                                'type' => 'repeater',
                                'instructions' => 'De slider laat 3 items per slide zien. Zorg dat het aantal items deelbaar is door 3.',
                                'required' => 0,
                                'conditional_logic' => array(
                                    array(
                                        array(
                                            'field' => 'field_5c4a085f7953e',
                                            'operator' => '==',
                                            'value' => '2',
                                        ),
                                    ),
                                ),
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'collapsed' => '',
                                'min' => 0,
                                'max' => 0,
                                'layout' => 'table',
                                'button_label' => '',
                                'sub_fields' => array(
                                    array(
                                        'key' => 'field_5c4a0a08ee830',
                                        'label' => 'Nieuws item',
                                        'name' => 'news_item',
                                        'type' => 'post_object',
                                        'instructions' => '',
                                        'required' => 0,
                                        'conditional_logic' => 0,
                                        'wrapper' => array(
                                            'width' => '',
                                            'class' => '',
                                            'id' => '',
                                        ),
                                        'post_type' => array(
                                            0 => 'post',
                                        ),
                                        'taxonomy' => '',
                                        'allow_null' => 0,
                                        'multiple' => 0,
                                        'return_format' => 'object',
                                        'ui' => 1,
                                    ),
                                ),
                            ),
                        ),
                        'min' => '',
                        'max' => '',
                    ),
                    'layout_5c4a065576f71' => array(
                        'key' => '5c4a065576f71',
                        'name' => 'text',
                        'label' => 'Tekst (1 blok per rij)',
                        'display' => 'block',
                        'sub_fields' => array(
                            array(
                                'key' => 'field_5c4a0666c3bab',
                                'label' => 'Tekst',
                                'name' => 'text_content',
                                'type' => 'wysiwyg',
                                'instructions' => 'Het wordt afgeraden om in deze editor afbeeldingen in te voegen. Gebruik bij voorkeur het block "Afbeelding" hiervoor.',
                                'required' => 0,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'default_value' => '',
                                'tabs' => 'visual',
                                'toolbar' => 'full',
                                'media_upload' => 1,
                                'delay' => 0,
                            ),
                        ),
                        'min' => '',
                        'max' => '',
                    ),

当我创建或编辑帖子时,我可以查看和使用这些字段。 现在,我有一个脚本来导入帖子,我想以编程方式将字段Tekst (1 blok per rij)添加为新行。 我尝试使用add_sub_rowupdate_field,但都无法正常工作。

当我这样使用add_sub_row时,我总是得到false的返回值:

add_sub_row([ 'field_5c4a0645c3baa', 1, 'field_5c4a0666c3bab'], [
    'field_5c4a0666c3bab' => 'test'
], $post)

当我使用update_field时,确实收到响应,看起来好像已经添加了该字段,但是当我去wp-admin并打开帖子时,却看不到它:

update_field('field_5c4a0645c3baa', [
    'acf_fc_layout' => 'text',
    'text_content' => 'test'
], $post)

我应该如何使用这些方法?因为我曾尝试使用ACF手册,但我真的不太了解如何将其用于灵活的内容。

1 个答案:

答案 0 :(得分:0)

我已经联系了ACF支持。 他们告诉我:

  

感谢您与我们联系。

     

不幸的是,恐怕无法使用add_row添加   与灵活的内容字段一起使用。

     

但是,您可以使用update_field实施自定义解决方案   用于将sub_rows添加到灵活内容字段的函数。

     

开发人员已经准备好使用它了,这个选项可能会   在将来的更新中可用。

     

给您带来的不便表示歉意。

后来他们对此进行了回复:

  

感谢您的跟进。

     

理想情况下,您需要加载字段,修改字段值并   使用update_field更新字段。

     

请参阅支持论坛中的以下文章。

     

https://support.advancedcustomfields.com/forums/topic/issues-with-add_rows-and-flexible-content/

     

希望这会有所帮助。

该链接帮助我在灵活的内容中创建了新行。

使用此代码:

$row_i = add_row(
    'field_55ce0e107c687', 
    [
        'acf_fc_layout' => 'headline', 
        'field_55df18296e4b3' => 'headline text'
    ],
    $ID
);

所以我不得不将其调整为我的字段名称:

add_row(
    'field_5c4a0645c3baa', [
    'acf_fc_layout'       => 'text',
    'field_5c4a0666c3bab' => $block[ 'value' ],
], $post);

现在可以正常工作了。