wp_get_attachment_image

SummerySummery

Get an HTML img element representing an image attachment

Syntax

wp_get_attachment_image( int $attachment_id, string|array $size = 'thumbnail', bool $icon = false, string|array $attr = '' )

DescriptionDescription

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.

ParametersParameters

$attachment_id

(int) (Required) Image attachment ID.

$size

(string|array) (Optional) Image size. Accepts any valid image size, or an array of width and height values in pixels (in that order).

Default value: 'thumbnail'

$icon

(bool) (Optional) Whether the image should be treated as an icon.

Default value: false

$attr

(string|array) (Optional) Attributes for the image markup.

  • 'src'
    (string) Image attachment URL.
  • 'class'
    (string) 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.
  • 'alt'
    (string) Image description for the alt attribute.
  • 'srcset'
    (string) The 'srcset' attribute value.
  • 'sizes'
    (string) The 'sizes' attribute value.

Default value: ''

ReturnReturn

(string) HTML img element or empty string on failure.

SourceSource

File: wp-includes/media.php


			

ChangelogChangelog

Changelog
Version Description
2.5.0 Introduced.
Scroll to Top