使用中继器图像字段上传图像到ACF


WordPress: Upload images to ACF using repeater image field

我的HTML表单允许用户选择任意数量的图像上传到自定义帖子类型。我在ACF中创建了一个名为"images"的中继器字段,属于"image"对象。

如何将上传的图像保存到该中继器字段?

这是我目前拥有的:

$files = $_FILES['file'];
$image_number = 1;
foreach ($files['name'] as $key => $value) {
  if ($files['name'][$key]) {
    $file = array(
      'name'     => $files['name'][$key],
      'type'     => $files['type'][$key],
      'tmp_name' => $files['tmp_name'][$key],
      'error'    => $files['error'][$key],
      'size'     => $files['size'][$key]
    );
    $uploaded_file = wp_handle_upload($file, array('test_form' => FALSE));
    update_sub_field( array('images', $image_number++, 'image'), $uploaded_file, $post_id);
  }
}

但是这些图像不会被保存。任何帮助感激,谢谢!

在我看来,你对update_field函数的使用,尤其是它的签名是错误的。你把参数弄混了。Wordpress ACF:如何通过自定义代码(PHP)向附加到用户的中继器字段添加行

试试下面的代码。这对我来说很有效。虽然这是一个旧的帖子,但它可能会帮助别人在未来。

$attachment = array(
    'guid'           => $upload_dir . '/' . basename( $filename ),
    'post_mime_type' => $filetype['type']
);
$attachment_id = wp_insert_attachment($attachment, $filename, $parent_post_id);
$row = array(
        'name' => "Hello",
        'logo' => $attachment_id,  // this is where you put post id of your image
        'website' => "http://hello.com"
    );
add_row( "repeater_field", $row, $blog_post_id);

我还注意到add_row仅在您已经至少有一行时才起作用。但如果想添加新行,请使用update_field。看看这里:add_row在ACF Pro isn't保存中继器值