wp_get_attachment_image


wp_get_attachment_image ( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' )

Parameters:
  • (int) attachment_id Image attachment ID.
  • (string|int[]) size Optional. Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default 'thumbnail'.
  • (bool) icon Optional. Whether the image should be treated as an icon. Default false.
  • (string|array) attr { Optional. Attributes for the image markup. @type string $src Image attachment URL. @type string $class CSS class name or space-separated list of classes. Default `attachment-$size_class size-$size_class`, where `$size_class` is the image size being requested. @type string $alt Image description for the alt attribute. @type string $srcset The 'srcset' attribute value. @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(). @type string $decoding The 'decoding' attribute value. Possible values are 'async' (default), 'sync', or 'auto'. Passing false or an empty string will result in the attribute being omitted. }
Returns:
  • (string) HTML img element or empty string on failure.
Defined at:
Change Log:
  • 4: .
  • 5: .
  • 6: .

Description

Gets an HTML img element representing an image attachment.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.

Related Functions

wp_get_attachment_image_url, wp_get_attachment_image_src, wp_get_attachment_image_sizes, wp_get_attachment_image_srcset, wp_get_attachment_link

Top Google Results

User discussions

wpseek mobile