Php WordPress:使用repeater图像字段将图像上载到ACF
我的HTML表单允许用户选择任意数量的图片上传到自定义帖子类型。我在ACF中创建了一个名为“images”的repeater字段,其中包含“image”对象 如何将上传的图像保存到该转发器字段中 这就是我目前拥有的:Php WordPress:使用repeater图像字段将图像上载到ACF,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,我的HTML表单允许用户选择任意数量的图片上传到自定义帖子类型。我在ACF中创建了一个名为“images”的repeater字段,其中包含“image”对象 如何将上传的图像保存到该转发器字段中 这就是我目前拥有的: $files = $_FILES['file']; $image_number = 1; foreach ($files['name'] as $key => $value) { if ($files['name'][$key]) { $file = array(
$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\u字段
函数,尤其是它的签名是错误的。你把参数弄混了。看看这里:试试下面的代码。它对我有用。虽然这是一篇老文章,但它可能会在将来帮助其他人
$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_行才起作用。但如果要添加新行,请使用update\u字段。请看这里: