以编程方式在灵活的内容中插入转发器行
Insert repeater rows in flexible content programmatically
如题所示,我有这样的结构:
flexible_content (Flexible content)
-accordion (Layout)
--accordion_item (Repeater)
---title (text)
---content (wysiwyg)
我有一个外部数据库,我想从中插入一些手风琴。如何使用 PHP 以编程方式执行此操作?我在从数据库获取数据后创建了一个循环,并希望 运行 在循环中插入功能。
我手动添加了 "accordion" 布局。现在我想在里面添加手风琴。我尝试使用 add_rows
和 update_field
,但无法成功。
这里是代码尝试:
foreach ($posts as $key => $value) {
$field_key = "field_5cab37b5c28y6"; // flexible_content field key
$value = array(
array( "content" => $value['content'], "acf_fc_layout" => "accordion" ),
array( "title" => $value['title'], "acf_fc_layout" => "accordion" ),
);
update_field( $field_key, $value, $post_id );
}
我设法找到了一种方法来做到这一点。这是我的代码:
<?php
$wp_page_id = 745;
//I have a table of data which I got from another non-wordpress site
$external_page_id = "247";
$sql = "SELECT data FROM `DataObject` WHERE `parent` = '$external_page_id' AND `public` = '1'";
$mydata = $wpdb->get_results($sql);
if ( !empty($mydata) ) {
$fc_data = array(
'flexible_content_field_key' => 'field_5dbc1c6d966de',
'flexible_content_layout' => 'accordion',
'flexible_content_repeater_field_key' => 'field_5dc52a904eb2b',
);
// making the array of data for repeater fields inside flexible content
$tmp = array();
foreach ($mydata as $key => $value) {
$json = json_decode( $value->data, true );
$data_to_insert = array(
'field_5dc52aa64eb2c' => $json['title'], // title field in repeater
'field_5dc52ae44eb2d' => $json['accordion-content'] // content field in repeater
);
array_push($tmp, $data_to_insert);
}
// got final array of all repeater field data. Now putting it into the final array of data
$fc_data['data_to_insert'] = $tmp;
// making the final array of data
$fc_rows = array(
array(
'acf_fc_layout' => $fc_data['flexible_content_layout'],
$fc_data['flexible_content_repeater_field_key'] => $tmp
)
);
// now insert the array of data
update_field($fc_data['flexible_content_field_key'], $fc_rows, $wp_page_id);
}
如题所示,我有这样的结构:
flexible_content (Flexible content)
-accordion (Layout)
--accordion_item (Repeater)
---title (text)
---content (wysiwyg)
我有一个外部数据库,我想从中插入一些手风琴。如何使用 PHP 以编程方式执行此操作?我在从数据库获取数据后创建了一个循环,并希望 运行 在循环中插入功能。
我手动添加了 "accordion" 布局。现在我想在里面添加手风琴。我尝试使用 add_rows
和 update_field
,但无法成功。
这里是代码尝试:
foreach ($posts as $key => $value) {
$field_key = "field_5cab37b5c28y6"; // flexible_content field key
$value = array(
array( "content" => $value['content'], "acf_fc_layout" => "accordion" ),
array( "title" => $value['title'], "acf_fc_layout" => "accordion" ),
);
update_field( $field_key, $value, $post_id );
}
我设法找到了一种方法来做到这一点。这是我的代码:
<?php
$wp_page_id = 745;
//I have a table of data which I got from another non-wordpress site
$external_page_id = "247";
$sql = "SELECT data FROM `DataObject` WHERE `parent` = '$external_page_id' AND `public` = '1'";
$mydata = $wpdb->get_results($sql);
if ( !empty($mydata) ) {
$fc_data = array(
'flexible_content_field_key' => 'field_5dbc1c6d966de',
'flexible_content_layout' => 'accordion',
'flexible_content_repeater_field_key' => 'field_5dc52a904eb2b',
);
// making the array of data for repeater fields inside flexible content
$tmp = array();
foreach ($mydata as $key => $value) {
$json = json_decode( $value->data, true );
$data_to_insert = array(
'field_5dc52aa64eb2c' => $json['title'], // title field in repeater
'field_5dc52ae44eb2d' => $json['accordion-content'] // content field in repeater
);
array_push($tmp, $data_to_insert);
}
// got final array of all repeater field data. Now putting it into the final array of data
$fc_data['data_to_insert'] = $tmp;
// making the final array of data
$fc_rows = array(
array(
'acf_fc_layout' => $fc_data['flexible_content_layout'],
$fc_data['flexible_content_repeater_field_key'] => $tmp
)
);
// now insert the array of data
update_field($fc_data['flexible_content_field_key'], $fc_rows, $wp_page_id);
}