14+ years building on WordPress / Replies in under 5 hours
WordPress 1 min read · Updated July 2026

How to use wp_get_attachment_image ( ) in WordPress Templates

AK
Ajay Khandal
WordPress Developer
TL;DR

wp_get_attachment_image() is the built-in WordPress function for outputting an image in a theme template, taking the attachment ID, a size, and optional HTML attributes as arguments. It only works for images already uploaded to the WordPress Media Library, not images pulled from inside post content. It's the standard, reliable way developers display featured or custom-field images in templates without hardcoding image tags by hand.

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.

Looking to round out your WordPress knowledge? Learn more about how to get the title in a WordPress template page. Learn more about adding a title tag to images in Elementor.

AK

Written by Ajay Khandal

WordPress Developer — building, fixing and speeding up WordPress sites.

Work with me →