How to use wp_get_attachment_image ( ) in WordPress Templates
By Aj Khandal | Published: | 1 min read
Displaying an image on a page or post is a very common task. To output an image in a template, we can use a pre-defined function from the WordPress library:
wp_get_attachment_image( int $attachment_id, string|array $size = 'thumbnail', bool $icon = false, string|array $attr = '' )
Parameters
$attachment_id (int, required)
- The 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:
'thumbnail'.
$icon (bool, optional)
- Whether the image should be treated as an icon. Default:
false.
$attr (string|array, optional)
- Attributes for the image markup. Default:
''.
This function only retrieves an image that was uploaded to WordPress — it does not output an image from the content of the post.