WordPress:使用转发器图像字段将图像上传到ACF

时间:2015-09-08 15:50:53

标签: php wordpress advanced-custom-fields

我的HTML表单允许用户选择要上传到自定义帖子类型的任意数量的图像。我在ACF中创建了一个名为' images' 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);
  }
}

但图像并未保存。感谢任何帮助,谢谢!

2 个答案:

答案 0 :(得分:0)

据我所知,您使用update_field功能尤其是它的签名是错误的。你混淆了参数。看看这里:Wordpress ACF: How to add rows to a repeater field attached to a user via custom code (PHP)

答案 1 :(得分:0)

尝试以下代码。它对我有用。虽然它是一个老帖子,但它可能在将来帮助其他人。

$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 in ACF Pro isn't saving repeater values