使用add_row以编程方式将条目添加到wordpress的高级自定义字段中的灵活内容字段

问题描述:

Wondering how to use add_rows (or something similar) to add an entry to a flexible content field programmatically. On ACF's website they mention add_rows can be used to add a row to a flexible content field https://www.advancedcustomfields.com/resources/ but they give no examples how to do it with a flexible content field; only with a repeater field. Thanks!

想知道如何使用add_rows(或类似的东西)以编程方式将条目添加到灵活的内容字段。 在ACF的网站上,他们提到add_rows可用于向灵活的内容字段添加一行 https://www.advancedcustomfields .com / resources / 但他们没有举例说明如何使用灵活的内容字段来做到这一点; 只有一个转发器字段。 谢谢! p> div>

use add_row( $selector, $value, $post_id ) This function will add a new row of data to an existing repeater field field value.

$selector: (required) The parent field name or key

$value: (required) The new value to append

$post_id: (optional) The post ID of which the value is saved to. Defaults to current post Return

Below is the example that how you can add image filed with multiple values

<?php 

$row = array(
    'image' => 123,
    'alt'   => 'Another great sunset',
    'link'  => 'http://website.com'
);

$i = add_row('images', $row);

?>

To Add repeater field in flexible content you should use below code :

<?php 
$field_key = "flexible_content_field_key";
$value = array(
    array( "sub_field_1" => "Foo1", "sub_field_2" => "Bar1", "acf_fc_layout" => "layout_1_name" ),
    array( "sub_field_x" => "Foo2", "sub_field_y" => "Bar2", "acf_fc_layout" => "layout_2_name" )
);
update_field( $field_key, $value, $post_id );

?>

Here acf_fc_layout is used to add sub fields for image here image is flexible content field key where in your case there will be your flexible content key.