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.

Share Improve this question edited Feb 23, 2018 at 21:04 Howdy_McGee 20.8k24 gold badges91 silver badges175 bronze badges asked Feb 23, 2018 at 20:50 Radley SustaireRadley Sustaire 1,3813 gold badges14 silver badges21 bronze badges 6
  • Why are you changing thumbnail image size from 150x150 to 185x125 instead of introducing a new size? thumbnail is a wordpress default image-size. – admcfajn Commented Feb 23, 2018 at 21:26
  • Is this a woocommerce site? – BenB Commented Feb 23, 2018 at 21:52
  • 1 @admcfajn Because the theme I'm building doesn't use 150x150 but isn't that irrelevant? – Radley Sustaire Commented Feb 24, 2018 at 3:19
  • @BenB Yeah it is woocommerce – Radley Sustaire Commented Feb 24, 2018 at 3:20
  • @admcfajn You can edit the thumbnail size in settings. It's perfectly reasonable to change it. – Jacob Peattie Commented Feb 24, 2018 at 3:29
 |  Show 1 more comment

1 Answer 1

Reset to default 0

Try 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