在wordpress中从前端多图像上传 [英] multiple image upload from frontend in wordpress

查看:439
本文介绍了在wordpress中从前端多图像上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CPTAnnonce,应该有四张附加的图片。现在我构建了一个前端表单,允许用户提交包含四个图像上传字段的帖子。当我尝试使用多张图片提交表格时,只有最后一张图片显示为仪表板中的特色。我在媒体部分看到了其他图片,但在帖子编辑页面中看不到,因此我可以更改或删除主题。



我在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'=> $文件['size'] [$ key]
);

$ _FILES = array(upload_attachment=> $ file);

foreach($ _FILES as $ file => $ array){
$ newupload = insert_attachment($ file,$ post_id);
}
}
}
}
...

< label for =postPhoto1>照片1:< /标签>
< input type =filename =upload_attachment []id =postPhoto1>

< label for =postPhoto1>照片2:< / label>
< input type =filename =upload_attachment []id =postPhoto2>

和functions.php我有这个函数:

 函数insert_attachment($ file_handler,$ post_id,$ setthumb ='false'){

//检查以确保其成功上传
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);
返回$ attach_id;
}

使用此表单可以成功上传图片,但只有最后一张图片会被设置为精选图像,我想在帖子编辑页面显示其他图像。



有什么帮助吗?

解决方案

试试这个

 < input type =fileid =attachmentname =attachment []multiple =multiple> 


I have a CPT "Annonce" which is supposed to have four attached images. Now I building a frontend form to allow users to submit posts with four image upload fields. When I try to submit the form with multiple images, only the last image appears as featured in the dashboard. I see the other images in Media section but not in the post edit page so I can change or delete theme.

I'm using this code in 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">

and in functions.php I have this function:

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;
}

Using this form the images are successfully uploaded but only last image get set as featured image, and I want to show other images in the post edit page too.

any help?

解决方案

Try this

 < input type="file" id="attachment" name="attachment[]" multiple="multiple" >

这篇关于在wordpress中从前端多图像上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆