2013-06-22 33 views
1

嗨,我要上傳多張與wp_insert_post但多個上傳的只有最後一個圖像插入有其他上傳圖片,但din't附加有....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/

回答

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; 

} 
+0

這不起作用 – Tsea

1

每次agp_process_wooimage正在運行r取代某個帖子ID的元數據:

帖子ID = 33'_product_image_gallery'的元數據只有最後一個文件在那裏,因爲之前的文件已被替換。

希望幫助;-)

+0

所以我怎麼能解決這個問題 – Corlax

+0

在最好的情況下給了元數據「_product_image_gallery」的編號或保存數組,而不是一個值。 $ array = get_post_meta($ post_id,'_product_image_gallery'); $ array = array_push($ array,$ attachment_id); update_post_meta($ post_id,$ array); – theode

+0

功能agp_process_wooimage($文件$ POST_ID,$ setthumb = '假'){ \t \t 如果($ _FILES [$文件] [ '錯誤'] == 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); $ array = get_post_meta($ post_id,'_product_image_gallery'); $ array = array_push($ array,$ attachment_id); if($ setthumb)update_post_meta($ post_id,$ array); return $ attachment_id; } – Corlax