gpt4 book ai didi

woocommerce-rest-api - WooCommerce REST API - 如何获取产品调整大小的图片网址?

转载 作者:行者123 更新时间:2023-12-04 14:41:25 25 4
gpt4 key购买 nike

wordpress 中的所有图像都将自动调整大小并保存在上传文件夹中,如下所示:

sample_product_image-100x100.png
sample_product_image-150x150.png
sample_product_image-180x113.png
sample_product_image-300x189.png
...
sample_product_image-555x349.png
sample_product_image-600x378.png
sample_product_image.png (original large file)

为了更快地加载,我需要在rest api中使用更小尺寸的产品图像,如下所示:
sample_product_image-300x189.png

但是 woocommerce rest api 只是向我发送原始最大文件(sample_product_image.png):
"images": [
{
"id": 7291,
"date_created": "2018-06-12T03:17:03",
"date_created_gmt": "2018-06-11T13:47:03",
"date_modified": "2018-06-12T03:17:03",
"date_modified_gmt": "2018-06-11T13:47:03",
"src": "http://example.com/wp-content/uploads/2018/06/sample_product_image.png",
"name": "sample_product_image",
"alt": "",
"position": 0
},

如何在 wc rest api 中获得较小的图像 url?

顺便说一句,我找到了 this plugin对于不适用于 woocommerce 的 wordpress rest api。

最佳答案

找到解决方案 here .
只需要将此过滤器添加到您的主题的function.php

function prepare_product_images($response, $post, $request) {
global $_wp_additional_image_sizes;

if (empty($response->data)) {
return $response;
}

foreach ($response->data['images'] as $key => $image) {
$image_urls = [];
foreach ($_wp_additional_image_sizes as $size => $value) {
$image_info = wp_get_attachment_image_src($image['id'], $size);
$response->data['images'][$key][$size] = $image_info[0];
}
}
return $response;

}

add_filter("woocommerce_rest_prepare_product_object", "prepare_product_images", 10, 3);

此外,如果您只需要 1 个特殊尺寸,您可以删除第二个 foreach 并手动初始化 $size'thumbnail' , 'medium' , 'medium_large''large'

关于woocommerce-rest-api - WooCommerce REST API - 如何获取产品调整大小的图片网址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50847382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com