admin管理员组文章数量:1276693
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?
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?
- that parameter does not add classes, it sets the classes, you can't use it to add classses, only replace/set them. What's the problem that required you to do this though? Can you provide the context? It looks like you devised a solution and asked how to fix it rather than asking about the original problem, if you share that then there may be alternatives you hadn't considered – Tom J Nowell ♦ Commented Oct 7, 2021 at 10:21
- Ah, okay, didn't know that it actually replaces the image size with a custom class. The context? I basically wanted to add an image size AND bottom margin to an image and thought it'd be more efficient to apply the custom class containing the bottom margin directly to the <img> element, instead of wrapping the <img> inside a <div> and then adding a class to that <div>, but seems there's not other way then? – Tatex Commented Oct 7, 2021 at 12:51
- usually there's some CSS pseudo-selector that can fix these issues but without knowing why you want to add a margin I can't advise. e.g. is this the last item in a list? Or a specific image in a particular layout? You've hidden most of the context and information in the process of trying to make this general/generic so it's very difficult to suggest something. Likewise there are other options from the PHP end but they depend on information/context you haven't shared – Tom J Nowell ♦ Commented Oct 7, 2021 at 13:09
1 Answer
Reset to default 1Just as Tom mentioned in the comments above. The fourth parameter of wp_get_attachment_image
sets the value, not adds to it so it will override the auto generated class.
If you need the size class and your custom class you can simply add both of them manually like so...
echo wp_get_attachment_image( $image_id, 'image-size', '', [
'class' => 'some-class attachment-image-size size-img-size'
] );
I
本文标签: imageswpgetattachmentimage function size argument not working if I also add a custom class
版权声明:本文标题:images - wp_get_attachment_image function size argument not working if I also add a custom class 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741279969a2369950.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论