简体   繁体   English

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

[英]WordPress: Upload images to ACF using repeater image field

My HTML form allows users to select any number of images to be uploaded to a custom post type. 我的HTML表单允许用户选择任意数量的图像以上传到自定义帖子类型。 I've created a repeater field in ACF called 'images', of 'image' objects. 我在ACF中创建了一个“图像”对象的转发器字段,称为“图像”。

How do I save the uploaded images into that repeater field? 如何将上传的图像保存到该转发器字段?

This is what I currently have: 这是我目前拥有的:

$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);
  }
}

But the images aren't saved. 但是图像不会保存。 Any help appreciated, thanks! 任何帮助表示赞赏,谢谢!

As far as I see, your use of the update_field function especially it's signature is wrong. 据我所知,您对update_field函数的使用尤其是其签名是错误的。 You mixed up the parameters. 您混合了参数。 Have a look here: Wordpress ACF: How to add rows to a repeater field attached to a user via custom code (PHP) 在这里看看: Wordpress ACF:如何通过自定义代码(PHP)将行添加到附加到用户的转发器字段中

Try the following code. 请尝试以下代码。 It is working for me. 它为我工作。 Although it's an old post, it might help someone else in future. 尽管这是一个过时的帖子,但将来可能会对其他人有所帮助。

$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);

I also noticed that add_row works only if you have already at least one row. 我还注意到,只有当您已经至少有一行时,add_row才起作用。 But if you want to add a new row, use update_field. 但是,如果要添加新行,请使用update_field。 Take a look here: add_row in ACF Pro isn't saving repeater values 在这里看看: ACF Pro中的add_row没有保存转发器值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM