Wordpress 将多个图像上载到Woocommerce产品
我正在尝试将各种图像从URL上传到给定的Wordpress 将多个图像上载到Woocommerce产品,wordpress,image,woocommerce,Wordpress,Image,Woocommerce,我正在尝试将各种图像从URL上传到给定的woocommerce产品。我的代码面临的问题是,尽管我看到图像正在上传到服务器,但当我去看我的文章时,我只看到上传到图库的最后一幅图像 这是我的密码: function generateSecondImage($image_url, $post_id) { $upload_dir = wp_upload_dir(); $image_data = file_get_contents($image_url); $filename =
woocommerce
产品。我的代码面临的问题是,尽管我看到图像正在上传到服务器,但当我去看我的文章时,我只看到上传到图库的最后一幅图像
这是我的密码:
function generateSecondImage($image_url, $post_id)
{
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);
if (wp_mkdir_p($upload_dir['path'])) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit',
);
$attach_id = wp_insert_attachment($attachment, $file, $post_id);
require_once ABSPATH . 'wp-admin/includes/image.php';
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
$res1 = wp_update_attachment_metadata($attach_id, $attach_data);
$res3 = update_post_meta($post_id, '_product_image_gallery', $attach_id);
}
下面是我如何调用函数的
for ($j=1; $j <$picCount ; $j++) {
generateSecondImage($pic[$j]->url, $post_id);
}
for($j=1;$j url,$post\u id);
}
我在想,也许,
res3
正在覆盖图库,只显示最后发布的图像,但如果是这样,我该如何告诉wordpress
将所有图像都包含在for循环中呢?终于解决了这个问题
这是我的最终代码。我的上传功能如下:
function generateSecondImage($image_url, $post_id)
{
$upload_dir = wp_upload_dir();
$image_data = file_get_contents($image_url);
$filename = basename($image_url);
if (wp_mkdir_p($upload_dir['path'])) {
$file = $upload_dir['path'] . '/' . $filename;
} else {
$file = $upload_dir['basedir'] . '/' . $filename;
}
file_put_contents($file, $image_data);
$wp_filetype = wp_check_filetype($filename, null);
$attachment = array(
'post_mime_type' => $wp_filetype['type'],
'post_title' => sanitize_file_name($filename),
'post_content' => '',
'post_status' => 'inherit',
);
$attach_id = wp_insert_attachment($attachment, $file, $post_id);
require_once ABSPATH . 'wp-admin/includes/image.php';
$attach_data = wp_generate_attachment_metadata($attach_id, $file);
$res1 = wp_update_attachment_metadata($attach_id, $attach_data);
echo "Attach ID is".$attach_id;
return $attach_id;
}
然后我必须创建一个以逗号分隔的ID列表,并将其添加到add\u post\u meta
因此,我的循环更改为:
for ($j=1; $j <$picCount ; $j++) {
$attarray[] = generateSecondImage($pic[$j]->url, $post_id);
}
$commaList = implode(', ', $attarray);
$res3 = add_post_meta($post_id, '_product_image_gallery', $commaList);
for($j=1;$j url,$post\u id);
}
$commaList=内爆(',',$attarray);
$res3=添加帖子元($post\u id,''u product\u image\u gallery',$commaList);
希望这能帮助其他正在寻找解决方案的人。
update\u post\u meta()
覆盖密钥并add\u post\u meta()
添加新行,因此请尝试add\u post\u meta()
谢谢,不幸的是,这不起作用。WooCommerce已经支持一个产品的多图像库。你为什么要推出你自己的版本?你是什么意思?我正在创建一个cron,它通过外部API从json获取图像,我需要将它们上传到WooCommerce。有没有其他方法可以实现这一目标?