admin管理员组文章数量:1122846
I have the following sizes registered, which I have verified with Regenerate Thumbnails.
thumbnail => 185x125 (crop)
shop_thumbnail => 150x62 (crop)
alm-thumbnail => 150x150 (crop)
Using the following code, I get the wrong size:
$test = wp_get_attachment_image_src( $id, 'shop_thumbnail' );
/*
$test = array(
"http://.../2018/02/underwear-150x150.png",
62,
62
)
*/
But if I use this function to get metadata, the custom size is clearly defined...
$test = wp_get_attachment_metadata( $id );
/*
$test = array(
["width"]=> int(177)
["height"]=> int(436)
["file"]=> string(21) "2018/02/underwear.png"
["sizes"]=> array(
"thumbnail"]=>
array(4) {
["file"]=> string(21) "underwear-177x125.png"
["width"]=> int(177)
["height"]=> int(125)
["mime-type"]=> string(9) "image/png"
}
// ...some other sizes
["shop_thumbnail"]=>
array(4) {
["file"]=> string(20) "underwear-150x62.png"
["width"]=> int(150)
["height"]=> int(62)
["mime-type"]=> string(9) "image/png"
}
)
As you can see, shop_thumbnail does exist in the second example but isn't what is being returned in the first example. I did check to make sure the file 150x62 exists, and it does.
Why is wp_get_attachment_image_src returning the wrong size? It appears to be using alm-thumbnail even though ajax load more isn't even used on this page.
Lots of "possible duplicates" say I need to define $content_width, but I couldn't get that to fix the problem.
Update: Changing 'shop_thumbnail' to array(150, 62)
works, but that's not ideal since shop_thumbnail is used throughout WooCommerce templates. I'd still like to figure this out.
I have the following sizes registered, which I have verified with Regenerate Thumbnails.
thumbnail => 185x125 (crop)
shop_thumbnail => 150x62 (crop)
alm-thumbnail => 150x150 (crop)
Using the following code, I get the wrong size:
$test = wp_get_attachment_image_src( $id, 'shop_thumbnail' );
/*
$test = array(
"http://.../2018/02/underwear-150x150.png",
62,
62
)
*/
But if I use this function to get metadata, the custom size is clearly defined...
$test = wp_get_attachment_metadata( $id );
/*
$test = array(
["width"]=> int(177)
["height"]=> int(436)
["file"]=> string(21) "2018/02/underwear.png"
["sizes"]=> array(
"thumbnail"]=>
array(4) {
["file"]=> string(21) "underwear-177x125.png"
["width"]=> int(177)
["height"]=> int(125)
["mime-type"]=> string(9) "image/png"
}
// ...some other sizes
["shop_thumbnail"]=>
array(4) {
["file"]=> string(20) "underwear-150x62.png"
["width"]=> int(150)
["height"]=> int(62)
["mime-type"]=> string(9) "image/png"
}
)
As you can see, shop_thumbnail does exist in the second example but isn't what is being returned in the first example. I did check to make sure the file 150x62 exists, and it does.
Why is wp_get_attachment_image_src returning the wrong size? It appears to be using alm-thumbnail even though ajax load more isn't even used on this page.
Lots of "possible duplicates" say I need to define $content_width, but I couldn't get that to fix the problem.
Update: Changing 'shop_thumbnail' to array(150, 62)
works, but that's not ideal since shop_thumbnail is used throughout WooCommerce templates. I'd still like to figure this out.
1 Answer
Reset to default 0Try this function:
woocommerce_get_product_thumbnail();
This seems to be the default function to get a product thumbnail, as you could see in wc code base By default it will return woocommerce_thumbnail but seems like it accepts other sizes in the first parameter.
本文标签: imagesWhy does wpgetattachmentimagesrc return the wrong sizewhen the correct size exists
版权声明:本文标题:images - Why does wp_get_attachment_image_src return the wrong size, when the correct size exists? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736289758a1928373.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
thumbnail
is a wordpress default image-size. – admcfajn Commented Feb 23, 2018 at 21:26