如何从前端添加多个产品图库图片

时间:2017-08-29 作者:Tcmxc

-我在经营一家woo Commenters商店

-用户可以使用wp insert post添加产品

-product gallery multiple image upload(产品库多幅图像上载)只会将最后一幅图像添加到帖子中,但在媒体中,它们都会附加到正确的帖子中

这是我的密码

作用php

function my_handle_attachment($file_handler,$post_id,$set_thu=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 ( is_numeric( $attach_id ) ) {

    update_post_meta( $post_id, \'_product_image_gallery\', $attach_id );

  }
  return $attach_id;  
}
前端

if ( $_FILES ) { 
        $files = $_FILES["my_file_upload"];  

        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 ("my_file_upload" => $file); 

                    $newupload = my_handle_attachment( "my_file_upload", $post_id);


                } 

            } 

    }
输入

<input type="file" name="my_file_upload[]" multiple="multiple" >
这将上载所有图像并将其附加到正确的帖子,但只有最后一幅图像会显示在产品库图像部分。我做错了什么?

2 个回复
最合适的回答,由SO网友:Tcmxc 整理而成
if ( ! empty( $_FILES[\'muti_files\'] )  ) {
            $files = $_FILES[\'muti_files\'];
            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("muti_files" => $file);
                $i=1;
                    foreach ($_FILES as $file => $array) {
                          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);
                            $vv .= $attachment_id . ",";
                            $i++;
                    }
                    update_post_meta($post_id, \'_product_image_gallery\',  $vv);
            }
        }
SO网友:Darwin

More optimized!

if ( ! empty( $_FILES[\'muti_files\'] )  ) {
        $files = $_FILES[\'muti_files\'];
        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("muti_files" => $file);
            $i=1;

require_once(ABSPATH . "wp-admin" . \'/includes/image.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/file.php\');
require_once(ABSPATH . "wp-admin" . \'/includes/media.php\');

                foreach ($_FILES as $file => $array) {
                      if ($_FILES[$file][\'error\'] !== UPLOAD_ERR_OK) __return_false();

                        $attachment_id = media_handle_upload($file, $post_id);
                        $vv .= $attachment_id . ",";
                        $i++;
                }
                update_post_meta($post_id, \'_product_image_gallery\',  $vv);
        }
    }
结束

相关推荐