2017-08-30 39 views
0

即時通訊使用wp_insert_post增加產品在woocommerces網站的前端如何使用wp_insert_post

我當前的代碼將上傳的所有圖像,但只有最後一個圖像將在產品圖庫圖片上傳多個圖像

繼承人我的代碼;

的functions.php

function my_handle_attachment($file_handler, $post_id, $set_thu=false) { 
    // check to make sure its a successful upload 

    if ($_FILES[$file_handler]['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'); 

    $attach_id = media_handle_upload($file_handler, $post_id); 


    if (is_numeric($attach_id)) { 

    update_post_meta($post_id, '_product_image_gallery', $attach_id); 

    } 
    return $attach_id; 
} 

前端

if ($_FILES) { 
    $files = $_FILES['upload_attachment']; 
    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("upload_attachment" => $file); 
      foreach ($_FILES as $file => $array) { 
       $newupload = my_handle_attachment($file,$post_id); 
      } 
     } 
    } 
} 


<input type="file" name="upload_attachment[]" multiple="multiple" /> 

我在做什麼錯在這裏?

+0

如果只有最後一個PIC是越來越保存,必須有在出現問題的每個循環,我相信。 –

+0

您是否看到任何問題?所有的圖像都在上傳,但只有最後一幅正在進入產品庫。 – Tsea

回答

0

你可以嘗試改變你的代碼,並嘗試一次,我記得這一點,而我的wordpress項目如此工作。

$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

+0

那麼我的代碼就是這樣做的,並將一個圖像添加到產品庫中。你還有什麼可以做到這一點? – Tsea

0

請儘量將

function my_handle_attachment($file_handler, $post_id) 
{ 
    if ($_FILES[$file_handler]['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'); 

$attach_id = media_handle_upload($file_handler, $post_id); 
if (is_numeric($attach_id)) 
{ 
update_post_meta($post_id, '_product_image_gallery', $attach_id); 
array_push($post_id, '_product_image_gallery', $attach_id) 
} 
return $attach_id; 
} 
+0

這沒有工作它做同樣的事情,我的代碼只有最後一個圖像顯示在產品庫部分。 – Tsea

相關問題