我有一個應該有四個附加圖像的CPT「Annonce」。現在我構建了一個前端表單,允許用戶提交包含四個圖像上傳字段的帖子。當我嘗試使用多張圖片提交表格時,只有最後一張圖片顯示爲儀表板中的特色。我在「媒體」部分看到了其他圖像,但看不到後期編輯頁面,因此我可以更改或刪除主題。從wordpress前端多圖像上傳
我使用這個代碼在頁面post.php中:
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 = insert_attachment($file,$post_id);
}
}
}
}
...
<label for="postPhoto1">Photo 1 :</label>
<input type="file" name="upload_attachment[]" id="postPhoto1">
<label for="postPhoto1">Photo 2 :</label>
<input type="file" name="upload_attachment[]" id="postPhoto2">
和functions.php中我有這樣的功能:
function insert_attachment($file_handler,$post_id,$setthumb='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 ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
return $attach_id;
}
使用這種形式的圖片上傳成功,但只有最後一張圖像被設置爲精選圖像,並且我想在帖子編輯頁面中顯示其他圖像。
有幫助嗎?