首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wp_insert_post多路上传图片

wp_insert_post多路上传图片
EN

Stack Overflow用户
提问于 2013-06-22 21:19:45
回答 2查看 3.6K关注 0票数 1

嗨,我想用wp_insert_post上传多个图像,但只有多个上传的最后一个图像插入其他图像上传,但不附加在那里.

以下是代码

代码语言:javascript
复制
<form action="#" method="post" enctype="multipart/form-data">  <input type="file" name="agp_gallery[]" id="agp_image_files" style="width:90%; margin-left:5px;" multiple>

下面是函数multiupload

代码语言:javascript
复制
          if ( $_FILES ) {
$files = $_FILES['agp_gallery'];
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("agp_gallery" => $file);

foreach ($_FILES as $file => $array) {

    agp_process_wooimage($file, $post_id, $result['caption']);
}
}
}
}

下面是main函数

代码语言:javascript
复制
function agp_process_wooimage($file, $post_id, $caption){

     if ($_FILES[$file]['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');

  $attachment_id = media_handle_upload($file, $post_id);

  update_post_meta($post_id, '_product_image_gallery', $attachment_id);

  $attachment_data = array(
    'ID' => $attachment_id,
    'post_excerpt' => $caption
  );

  wp_update_post($attachment_data);

  return $attachment_id;

}   

我从这里得到上面的代码:

http://madebyraygun.com/blog/2012/upload-and-attach-multiple-images-from-the-wordpress-front-end/

所有代码都在这里:

http://wp.tutsplus.com/tutorials/allow-users-to-submit-images-your-site/

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-26 02:57:08

请试试这个

代码语言:javascript
复制
function agp_process_wooimage($file, $post_id){

     if ($_FILES[$file]['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');

  $attachment_id = media_handle_upload($file, $post_id);
 update_post_meta($post_id,  array_push($post_id, '_product_image_gallery', $attachment_id));

  return $attachment_id;

}
票数 2
EN

Stack Overflow用户

发布于 2013-06-25 22:43:22

agp_process_wooimage每次运行时都会替换某个帖子id的元数据:

post id = 33 '_product_image_gallery‘的元数据中只有最后一个文件,因为之前的文件已经被替换了。

希望这能有所帮助;-)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17251206

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档