【问题标题】:multiple image upload from frontend in wordpress在wordpress中从前端上传多张图片
【发布时间】:2013-01-12 09:13:29
【问题描述】:

我有一个 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;
}

使用此表单成功上传图片,但只有最后一张图片被设置为特色图片,我也想在帖子编辑页面中显示其他图片。

有什么帮助吗?

【问题讨论】:

    标签: forms wordpress


    【解决方案1】:

    您可以安装高级自定义字段插件,创建任意数量的图像字段,然后按照文档中描述的方法在前端使用它。 http://www.advancedcustomfields.com/resources/tutorials/creating-a-front-end-form/

    我在 wordpress 3.4.2 中测试并使用了此方法,后来的版本在从前端上传时出现问题。

    【讨论】:

      【解决方案2】:

      试试这个

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-29
        • 2019-08-01
        • 1970-01-01
        • 2014-09-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多