images – wp_get_attachment_image function size argument not working if I also add a custom class


I’m trying to output some images the right way, using the wp_get_attachment_image function. Here’s how I do it:
echo wp_get_attachment_image( $image_id, 'image-size', '', [ 'class' => 'some-class' );

The thing is, it retrieves the image-size only (and outputs it as a class) if I use the first two arguments, $image_id and 'image-size', like so:
<img width="260" height="350" src="[image-src-link]" class="attachment-image-size size-img-size" alt="" loading="lazy" />

However, if I add a custom class as the fourth argument, as demonstrated in the first paragraph, it somehow overrides the image size and uses only the custom class, so this is what I get:
<img width="260" height="350" src="[image-src-link]" class="some-class" alt="" loading="lazy" />

I absolutely don’t get what I’m doing wrong. I found this thread, the note in the accepted answer implies that this is some kind of bug, but the thread is almost 8 years old, and I think the bug would’ve been resolved by now. Also, the WordPress Code Reference clearly states that the wp_get_attachment_image can take four arguments and says absolutely nothing about image size and custom class conflicts, so I’m officially out of ideas. Could somebody please steer me in the right direction?

Tatex 11 months 2021-10-07T04:54:34-05:00 0 Answers 0 views 0

Leave an answer