最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to upload images from wordpress front-end (the first image must be thumbnail and the rest without first must be in gallery w

programmeradmin2浏览0评论

How to set first image from $_FILES array to product woocommerce post thumbnail and the rest put into woocommerce product gallery.

I have this code:

if ( ! empty( $_FILES['thumbnail'] )  ) {
        $files = $_FILES['thumbnail'];
        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("thumbnail" => $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);
                update_post_meta($post_id,'_thumbnail_id',$attachment_id);
        }
    }

Code works, but the last (not first) image is product thumbnail and all (not rest) images are set to image gallery. Please help

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论