您好我想使用wp_insert_post上传多张图片,但只有多张上传的最后一张图片,其他图片已上传,但不会附在那里....
这是代码
<form action="#" method="post" enctype="multipart/form-data"> <input type="file" name="agp_gallery[]" id="agp_image_files" style="width:90%; margin-left:5px;" multiple>
这是函数multiupload
if ( $_FILES ) {
$files = $_FILES['agp_gallery'];
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]
);
$_FILES = array("agp_gallery" => $file);
foreach ($_FILES as $file => $array) {
agp_process_wooimage($file, $post_id, $result['caption']);
}
}
}
}
这是主要功能
function agp_process_wooimage($file, $post_id, $caption){
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attachment_id = media_handle_upload($file, $post_id);
update_post_meta($post_id, '_product_image_gallery', $attachment_id);
$attachment_data = array(
'ID' => $attachment_id,
'post_excerpt' => $caption
);
wp_update_post($attachment_data);
return $attachment_id;
}
我从这里得到以上代码: -
http://madebyraygun.com/blog/2012/upload-and-attach-multiple-images-from-the-wordpress-front-end/
和
这里的所有代码: -
http://wp.tutsplus.com/tutorials/allow-users-to-submit-images-your-site/
答案 0 :(得分:2)
请试试这个
function agp_process_wooimage($file, $post_id){
if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$attachment_id = media_handle_upload($file, $post_id);
update_post_meta($post_id, array_push($post_id, '_product_image_gallery', $attachment_id));
return $attachment_id;
}
答案 1 :(得分:1)
每次运行agp_process_wooimage时,它都会替换某个帖子ID的元数据:
发布id = 33&lt; _product_image_gallery&#39;的元数据只有最后一个文件在那里,因为之前的文件已被替换。
希望有所帮助; - )