我有一个CPT "Annonce“,它应该有四个附加的图像。现在,我构建了一个前端表单,允许用户提交带有四个图像上传域的帖子。当我尝试提交包含多个图像的表单时,只有最后一个图像显示在仪表板中。我在媒体部分看到了其他图像,但在编辑后的页面上看不到,所以我可以更改或删除主题。
我在page-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;
}使用此表单的图像被成功上传,但只有最后的图像被设置为特色图像,我想在编辑后页面显示其他图像也。
有什么帮助吗?
发布于 2013-03-15 20:11:23
您可以安装高级自定义字段插件,创建任意数量的镜像字段,然后按照文档中介绍的方法在前台使用。http://www.advancedcustomfields.com/resources/tutorials/creating-a-front-end-form/
这个方法是我在wordpress 3.4.2中测试和工作的,后来的版本在从前端上传时出现了问题。
发布于 2014-02-13 19:01:30
尝尝这个
< input type="file" id="attachment" name="attachment[]" multiple="multiple" >https://stackoverflow.com/questions/14291908
复制相似问题