On this page
wp_get_attachment_image( int $attachment_id, string|int[] $size = 'thumbnail', bool $icon = false, string|array $attr = '' ): string
Gets an HTML img element representing an image attachment.
Description
While $size will accept an array, it is better to register a size with add_image_size() so that a cropped version is generated. It’s much more efficient than having to find the closest-sized image and then having the browser scale down the image.
Parameters
$attachment_idint Required-
Image attachment ID.
$sizestring|int[] Optional-
Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default
'thumbnail'.Default:
'thumbnail' $iconbool Optional-
Whether the image should be treated as an icon.
Default:
false $attrstring|array Optional-
Attributes for the image markup.
srcstringImage attachment URL.classstringCSS class name or space-separated list of classes.
Defaultattachment-$size_class size-$size_class, where$size_classis the image size being requested.altstringImage description for the alt attribute.srcsetstringThe'srcset'attribute value.sizesstringThe'sizes'attribute value.loadingstring|falseThe'loading'attribute value. Passing a value of false will result in the attribute being omitted for the image.
Defaults to'lazy', depending on wp_lazy_loading_enabled() .decodingstringThe'decoding'attribute value. Possible values are'async'(default),'sync', or'auto'.
Default:
''
Return
string HTML img element or empty string on failure.
More Information
Usage
wp_get_attachment_image( $attachment_id, $size, $icon, $attr );
If the attachment is an image, the function returns an image at the specified size. For other attachments, the function returns a media icon if the $icon parameter is set to true.
To get attachment IDs dynamically in a template, you can use get_posts( array( 'post_type' => 'attachment' ) ), etc.
Source
File: wp-includes/media.php. View all references
function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {
$html = '';
$image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
if ( $image ) {
list( $src, $width, $height ) = $image;
$attachment = get_post( $attachment_id );
$hwstring = image_hwstring( $width, $height );
$size_class = $size;
if ( is_array( $size_class ) ) {
$size_class = implode( 'x', $size_class );
}
$default_attr = array(
'src' => $src,
'class' => "attachment-$size_class size-$size_class",
'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
'decoding' => 'async',
);
// Add `loading` attribute.
if ( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ) {
$default_attr['loading'] = wp_get_loading_attr_default( 'wp_get_attachment_image' );
}
$attr = wp_parse_args( $attr, $default_attr );
// If the default value of `lazy` for the `loading` attribute is overridden
// to omit the attribute for this image, ensure it is not included.
if ( array_key_exists( 'loading', $attr ) && ! $attr['loading'] ) {
unset( $attr['loading'] );
}
// Generate 'srcset' and 'sizes' if not already present.
if ( empty( $attr['srcset'] ) ) {
$image_meta = wp_get_attachment_metadata( $attachment_id );
if ( is_array( $image_meta ) ) {
$size_array = array( absint( $width ), absint( $height ) );
$srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id );
$sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id );
if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
$attr['srcset'] = $srcset;
if ( empty( $attr['sizes'] ) ) {
$attr['sizes'] = $sizes;
}
}
}
}
/**
* Filters the list of attachment image attributes.
*
* @since 2.8.0
*
* @param string[] $attr Array of attribute values for the image markup, keyed by attribute name.
* See wp_get_attachment_image().
* @param WP_Post $attachment Image attachment post.
* @param string|int[] $size Requested image size. Can be any registered image size name, or
* an array of width and height values in pixels (in that order).
*/
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
$attr = array_map( 'esc_attr', $attr );
$html = rtrim( "<img $hwstring" );
foreach ( $attr as $name => $value ) {
$html .= " $name=" . '"' . $value . '"';
}
$html .= ' />';
}
/**
* Filters the HTML img element representing an image attachment.
*
* @since 5.6.0
*
* @param string $html HTML img element or empty string on failure.
* @param int $attachment_id Image attachment ID.
* @param string|int[] $size Requested image size. Can be any registered image size name, or
* an array of width and height values in pixels (in that order).
* @param bool $icon Whether the image should be treated as an icon.
* @param string[] $attr Array of attribute values for the image markup, keyed by attribute name.
* See wp_get_attachment_image().
*/
return apply_filters( 'wp_get_attachment_image', $html, $attachment_id, $size, $icon, $attr );
}
Hooks
- apply_filters( 'wp_get_attachment_image',
string $html ,int $attachment_id ,string|int[] $size ,bool $icon ,string[] $attr ) -
Filters the HTML img element representing an image attachment.
- apply_filters( 'wp_get_attachment_image_attributes',
string[] $attr ,WP_Post $attachment ,string|int[] $size ) -
Filters the list of attachment image attributes.
Related
Uses
| Uses | Description |
|---|---|
| wp_get_loading_attr_default() wp-includes/media.php | Gets the default value to use for a |
| wp_lazy_loading_enabled() wp-includes/media.php | Determines whether to add the |
| wp_calculate_image_srcset() wp-includes/media.php | A helper function to calculate the image sources to include in a ‘srcset’ attribute. |
| wp_calculate_image_sizes() wp-includes/media.php | Creates a ‘sizes’ attribute value for an image. |
| wp_get_attachment_image_src() wp-includes/media.php | Retrieves an image to represent an attachment. |
| image_hwstring() wp-includes/media.php | Retrieves width and height attributes using given width and height values. |
| wp_get_attachment_metadata() wp-includes/post.php | Retrieves attachment metadata for attachment ID. |
| wp_parse_args() wp-includes/functions.php | Merges user defined arguments into defaults array. |
| absint() wp-includes/functions.php | Converts a value to non-negative integer. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| get_post_meta() wp-includes/post.php | Retrieves a post meta field for the given post ID. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
Used By
| Used By | Description |
|---|---|
| WP_Widget_Media_Image::render_media() wp-includes/widgets/class-wp-widget-media-image.php | Render the media on the frontend. |
| get_custom_logo() wp-includes/general-template.php | Returns a custom logo, linked to home unless the theme supports removing the link on the home page. |
| WP_Media_List_Table::column_title() wp-admin/includes/class-wp-media-list-table.php | Handles the title column output. |
| _wp_post_thumbnail_html() wp-admin/includes/post.php | Returns HTML for the post thumbnail meta box. |
| WP_Comments_List_Table::column_response() wp-admin/includes/class-wp-comments-list-table.php | |
| get_the_post_thumbnail() wp-includes/post-thumbnail-template.php | Retrieves the post thumbnail. |
| wp_get_attachment_link() wp-includes/post-template.php | Retrieves an attachment page link using an image or icon, if possible. |
| gallery_shortcode() wp-includes/media.php | Builds the Gallery shortcode output. |
| set_post_thumbnail() wp-includes/post.php | Sets the post thumbnail (featured image) for the given post. |
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_get_attachment_image