从前端在WordPress多图片上传(multiple image upload from fron

2019-10-29 15:41发布

我有一个CPT“Annonce”这是应该有四个附加的图像。 现在我建立一个前端的形式,以允许用户使用四个图像上传域提交帖子。 当我尝试使用多个图像提交表单,只有最后的图像出现在仪表盘为特色。 我看到其他的图像媒体部分,但不是在后期编辑页面,所以我可以更改或删除主题。

我使用在页面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;
}

使用这种形式的图片上传成功,但只有最后得到的图像设置为特色的形象,我想显示在后编辑页面其他图像了。

任何帮助吗?

Answer 1:

您可以安装先进的自定义字段插件,只要你想创造尽可能多的像场,然后在前端与文档中描述的方法使用它。 http://www.advancedcustomfields.com/resources/tutorials/creating-a-front-end-form/

此方法测试,并通过我在WordPress 3.4.2工作,后来的版本是从前端上传的问题。



Answer 2:

尝试这个

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


文章来源: multiple image upload from frontend in wordpress