1

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

这是代码

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

这是功能多重上传

          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']);
}
}
}
}

这是主要功能

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/

4

2 回答 2

2

请试试这个

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;

}
于 2013-06-25T18:57:07.883 回答
1

每次运行 agp_process_wooimage 时,它​​都会替换某个帖子 ID 的元数据:

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

希望有帮助;-)

于 2013-06-25T14:43:21.567 回答