2016-07-29 97 views
0

我有一個用於編輯自定義帖子的自定義表單,並且此表單用於前端用戶編輯其帖子和相應附件。對於附件部分,我有兩個輸入文件元素,可以上傳圖片和視頻,我到達下面的PHP代碼來捕獲上傳的附件。自定義帖子表單中的wordpress附件錯誤

  if ($_FILES) { 

     foreach ($_FILES as $file => $array) { 

      if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK || $_FILES[$file]['error'] === UPLOAD_ERR_NO_FILE) { 
       $ermg = "upload error : " . $_FILES[$file]['error']; 
       continue; 
      } else { 
       $attach_id = media_handle_upload($file, $post_id); 
       $type = get_post_mime_type($attach_id); 
       if ($type === 'image/jpeg' || $type === 'image/png') { 
        update_post_meta($new_post, '_thumbnail_id', $attach_id); 
       } elseif ($type === 'video/mp4' || $type === 'video/quickime') { 
        update_post_meta($new_post, '_video_id', $attach_id); 
       } 
      } 
      return $ermg; 
     } 
    } 

這裏的問題是我的代碼是不是如果兩者都在同一時間,那裏的上傳單個附件工作正常上傳捕捉只有一個附件。

除此之外,還有一個問題,我看到一個新的未知附件,每次上傳都會在媒體庫中形成標題,下面是屏幕截圖,不期望這個新的未知附件。任何人都可以幫忙嗎? enter image description here

+0

! ='我可以同時上傳兩個文件,但未知文件仍然被創建:( – krishna89

回答

0

試試這個:(在function.php)通過後,如果``從($ _FILES [$文件] [ '錯誤']``==`來改變運營商

require_once(ABSPATH.'wp-admin/includes/admin.php'); 
if ($_FILES) 
{ 
    foreach ($_FILES as $file => $array) 
    {  
     $file_return = wp_handle_upload($file, array('test_form' => false)); 
     if(isset($file_return['error']) || isset($file_return['upload_error_handler'])) 
     { 
      $ermg = "upload error : " . $_FILES[$file]['error']; 
      continue; 
     } 
     else 
     { 
      $filename=$file_return['file']; 
      $type=$file_return['type']; 
      $attachment=array(
       'post_mime_type' => $file_return['type'], 
       'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)), 
       'post_content' => '', 
       'post_status' => 'inherit', 
       'guid' => $file_return['url'] 
      ); 
      $attachment_id=wp_insert_attachment($attachment, $file_return['url']); 
      require_once(ABSPATH . 'wp-admin/includes/image.php'); 
      $attachment_data=wp_generate_attachment_metadata($attachment_id, $filename); 
      wp_update_attachment_metadata($attachment_id,$attachment_data); 
      if ($type === 'image/jpeg' || $type === 'image/png') { 
       update_post_meta($new_post, '_thumbnail_id', $attachment_id); 
      } elseif ($type === 'video/mp4' || $type === 'video/quickime') { 
       update_post_meta($new_post, '_video_id', $attachment_id); 
      } 
     } 
     return $ermg; 
    } 
} 
+0

我試過了你的代碼,但它不工作,未知的附件仍然被創建。 – krishna89

相關問題