从JSON文件填充ACF图像字段

时间:2021-04-22 作者:bjames

我想知道是否有ACF专业人士能在这里帮助我。我正在考虑从应用程序创建的外部JSON文件中自动填充包含ACF字段的自定义帖子。这将由Cron作业每30分钟更新一次。我想知道的是,因为每篇帖子都会有3到15张图片,如何最好地做到这一点?是否可以从JSON文件中的图像URL向ACF gallery字段添加图像?或者我需要为图像URL设置一个带有嵌入字段的转发器吗?还是有其他选择?

1 个回复
SO网友:joshmoto

是的,可以从json数据填充acf gallery字段。

假设这是你应用程序中的gallery json。。。

{
    "gallery" : [
        "https://i.imgur.com/VHkyr8P.jpeg",
        "https://i.imgur.com/obdqDwa.jpeg",
        "https://i.imgur.com/eQuSNVx.jpeg",
        "https://i.imgur.com/1lVyIJt.jpeg",
        "https://i.imgur.com/5uIOviX.jpeg"
    ]
}
现在,在cron作业中,您可以运行如下所示的函数来处理acf gallery字段图像更新。

下面的示例函数获取任何现有acf gallery图像,保留它们并将新图像合并到gallery字段。。。

/**
 * @param $post_id int
 * @param $acf_gallery_field_name string
 * @param $json_url string
 */
function add_json_imgs_to_acf_gallery($post_id, $acf_gallery_field_name, $json_url) {

    // get the json data via url
    $json = file_get_contents($json_url);

    // decode json to array
    $array = json_decode($json,true);

    // if decoded json is array and has array key gallery (as per above json example)
    if (is_array($array) && array_key_exists(\'gallery\')) {

        // lets begin multiple attachment array
        $attach_ids = [];

        // foreach of your json gallery urls
        foreach ($array[\'gallery\'] as $img_url) {

            // check the type of file
            // we\'ll use this as the \'post_mime_type\'
            $file_type = wp_check_filetype(basename($img_url),null);

            // get the path to the upload directory
            $wp_upload_dir = wp_upload_dir();

            // prepare an array of attachment post data
            $attachment = [
                \'guid\' => $wp_upload_dir[\'url\'] . \'/\' . basename($img_url),
                \'post_mime_type\' => $file_type[\'type\'],
                \'post_title\' => preg_replace(\'/\\.[^.]+$/\', \'\',basename($img_url)),
                \'post_content\' => \'\',
                \'post_status\' => \'inherit\'
            ];

            // insert the attachment
            $attach_id = wp_insert_attachment($attachment,$img_url,$post_id);

            // make sure that this file is included, as wp_generate_attachment_metadata() depends on it
            require_once(ABSPATH . \'wp-admin/includes/image.php\');

            // generate the metadata for the attachment, and update the database record
            $attach_data = wp_generate_attachment_metadata($attach_id,$img_url);
            wp_update_attachment_metadata($attach_id,$attach_data);

            // build attachment id\'s as an array
            $attach_ids[] = $attach_id;

        }

        // get our current acf gallery field images
        $gallery = get_field($acf_gallery_field_name,$post_id);

        // if we already have galley images
        if($gallery) {

            // empty array for existing gallery image id to be populated
            $existing_attach_ids = [];

            // for each gallery item as array key => image array
            foreach ($gallery as $key => $img) {

                // add image id to existing attachment ids 
                $existing_attach_ids[] = $img[\'ID\'];

            }

            // merge existing acf gallery ids with newly json uploaded image items
            $attach_ids = array_merge($existing_attach_ids,$attach_ids);

            // update acf gallery field with merged attachment ids
            update_field($acf_gallery_field_name,$attach_ids,$post_id);

        } else {

            // update acf gallery field with new attachment ids
            update_field($acf_gallery_field_name,$attach_ids,$post_id);

        }

    }

}