Summery Summery
Get an HTML img element representing an image attachment
Syntax Syntax
Description 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 Parameters
- $attachment_id
-
(Required) Image attachment ID.
- $size
-
(Optional) Image size. Accepts any valid image size, or an array of width and height values in pixels (in that order). Default 'thumbnail'.
Default value: 'thumbnail'
- $icon
-
(Optional) Whether the image should be treated as an icon.
Default value: false
- $attr
-
(Optional) Attributes for the image markup.
- 'src'
(string) Image attachment URL. - 'class'
(string) CSS class name or space-separated list of classes. Defaultattachment-$size_class size-$size_class
, where$size_class
is the image size being requested. - 'alt'
(string) Image description for the alt attribute. - 'srcset'
(string) The 'srcset' attribute value. - 'sizes'
(string) The 'sizes' attribute value.
Default value: ''
- 'src'
Return Return
(string) HTML img element or empty string on failure.
Source Source
File: wp-includes/media.php
* @type string $sizes The 'sizes' attribute value. * @type string|false $loading The '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(). * } * @return string HTML img element or empty string on failure. */ 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 = join( '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 ) ) ), ); // Add `loading` attribute. if ( wp_lazy_loading_enabled( 'img', 'wp_get_attachment_image' ) ) { $default_attr['loading'] = 'lazy'; } $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; } } } }
Advertisement
Changelog Changelog
Version | Description |
---|---|
2.5.0 | Introduced. |