1

正如标题所示,我有这种结构:

flexible_content (Flexible content)
-accordion (Layout)
--accordion_item (Repeater)
---title (text)
---content (wysiwyg)

我有一个外部数据库,我想从中插入一些手风琴。如何使用 PHP 以编程方式执行此操作?从数据库获取数据后,我创建了一个循环,并希望在循环内运行插入功能。
我手动添加了“手风琴”布局。现在我想在里面添加手风琴。我尝试使用add_rowsand 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 );
    }
4

1 回答 1

0

我设法找到了一种方法来做到这一点。这是我的代码:

<?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);
}
于 2019-11-19T10:59:29.243 回答