Php 以编程方式在灵活内容中插入转发器行
正如标题所示,我有这样的结构:Php 以编程方式在灵活内容中插入转发器行,php,wordpress,advanced-custom-fields,acfpro,Php,Wordpress,Advanced Custom Fields,Acfpro,正如标题所示,我有这样的结构: flexible_content (Flexible content) -accordion (Layout) --accordion_item (Repeater) ---title (text) ---content (wysiwyg) 我有一个外部数据库,我想从中插入一些手风琴。如何使用PHP以编程方式实现这一点?在从DB获取数据后,我创建了一个循环,并希望在循环中运行插入功能我手动添加了“手风琴”布局。现在我想在里面加上手风琴。我尝试了添加行和更新字段,
flexible_content (Flexible content)
-accordion (Layout)
--accordion_item (Repeater)
---title (text)
---content (wysiwyg)
我有一个外部数据库,我想从中插入一些手风琴。如何使用PHP以编程方式实现这一点?在从DB获取数据后,我创建了一个循环,并希望在循环中运行插入功能我手动添加了“手风琴”布局。现在我想在里面加上手风琴。我尝试了
添加行
和更新字段
,但未能成功。
以下是代码尝试:
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);
}
我设法找到了一种方法。这是我的密码:
<?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);
}
您能显示到目前为止您使用过的代码吗。@NigelRen添加了代码。您能显示到目前为止您使用过的代码吗。@NigelRen添加了代码。