If you only want to add an extra class, then you should use wp_get_attachment_image
. It has few extra params, and the last one is used for setting class names.
Sample usage:
<?php echo wp_get_attachment_image( get_the_ID(), 'thumbnail', "", ["class" => "my-custom-class"] ); ?>
The main advantage of this aproach is that you will also get the whole srcset
attributes for free.
Related Posts:
- How To Retrieve An Image Attachment’s Alt Text?
- Check if post has attachments (not image)
- Validate alt text for attachments?
- Applying automatic link class to images embedded to posts
- get post attachments of a particular size
- Change image link: wp_get_attachment_link
- Making images from single.php pointing to an attachment .php template
- wp_get_attachment_image_src always returns false
- Exact image sizes
- Get the size (size in Kb or MB) of an featured image?
- query attachment images … if no attachments -> get attachments of parent page?
- Edit default image attachment size
- Change code to display image attachment page
- Attachment 0×0 size error [closed]
- Add image attachment to post via coding?
- Removing width and height attributes from wp-get-attachment
- Get post attachment images dimension and use in embed code
- use wp_get_attachment_image() to show attachments
- Adding width and height to wp_get_attachment_image_src
- Get the bare URL for an attachment
- Display attachment resolution size
- How to check if there is image title, caption, description?
- How to remove attachments size attribute [duplicate]
- How to redirect ‘file url’ to ‘post url’ i.e. in attachment.php
- Check what attached image size is used
- How do I get all attachments that are used in the current post, not just the children?
- Clean up images, delete unused image sizes attached to a post type
- Get title of post image is attached to
- Correct attachment size
- Post Gallery list attachments except the one used as post thumbnail
- Get image attachment to a post outside the loop?
- How do I intercept and modify the functionality of wp_get_attachment_image()?
- Unable to get wp_get_attachment_url to show
- Get Attachment Image Class by attachment id
- Possible to add “Even/Odd Classes” to image attachments via wp_get_attachment_image?
- changing the default picture size link url
- Get an image from the media library. Struggling with the code?
- How to view exact image dimensions of a media in Admin view?
- Filter not applied to all image sizes
- wp_get_attachment_image_src calling wrong location
- Get attachment URL from attachment Link in WordPress
- Display setting ‘size’ not available in image settings modal
- Hide Image Container if No Image Attachments
- Incomplete attachment page
- How to retrieve “custom url” from image attachment in wordpress
- Retreive tags from attached post for media on page
- list of attachments: wp_get_attachment_link works, wp_get_attachment_image does not
- What is the most efficient way to retrieve a group of image URL’s from different attachment ID’s of a specific crop size?
- get_image_tag filter not working
- Change attachment inside the_content using filters
- How to get image title/alt attribute?
- How to manage attachment relationships for specific posts in WP 3.5+
- Programmatically adding images to media library
- Can I add a Category Metabox to attachment?
- Creating an Image-Centric Custom Post Type?
- How does WP media uploader create the 3 different sized images, and how can I duplicate it
- Programmatically get images by URL and save in uploads folder
- how to get original image using wp_get_attachment_image_src
- Can I attach image to post without adding it to post?
- Get the first image from post content (eg.: hotlinked images)
- Change attachment filename
- How to get attachment file name not attachment URL
- How to trigger a refresh in the media modal
- Remove Dimension from wp_get_attachment_image
- Get all post attachments except featured image
- Programmatically adding images to the media library with wp_generate_attachment_metadata randomly fails
- remove links from images using functions.php
- Uploaded images don’t show in Media Library if there are special characters in IPTC Keywords
- How can I bulk delete media and attachments using WP-CLI?
- Disable image attachment links
- wp_get_attachment_image_src and server path
- Getting a taxonomy’s thumbnail url
- How to cleanly delete attachment but retain original file?
- How to add a class to the attachment images
- Changing attachment urls?
- Add custom class to attachment in media library grid mode
- Are captions stored anywhere?
- Trying to hide buttons from Attachment window
- How to find attachment by it’s name?
- get_the_post_thumbnail() doesn’t taking style attribute
- Find the post an attachment is attached to
- Expanding new Media Uploader in WordPress 3.5
- How can I UN-attach media from a post?
- Uploading Multiple Attachments From Front-End With A Description
- complex restriction of items in media library
- Remove duplicate attachments
- List most recent image uploads, but only for specific custom post type
- Edit image / Delete image link
- How could I add button next to “edit image” button in “Attachment Details”
- How do I detach images from posts?
- Count the number of images uploded on the website
- How to get attachment caption (get_the_excerpt gives parent post excerpt) ?
- Is it possible to query specific WordPress Attachment files (.ppt & .pdf) and output them in a list?
- How do I delete all generated images from my server except those currently used in posts
- How to paginate attachments in a secondary query as gallery?
- media_sideload_image file name?
- Get all posts (of any post type) an attachment is used in
- Insert an image into a post by API
- Function to call the attachment image from post
- How to add classes to images based on their categories?