WordPress wp_get_attachment_image() Function

Definition
In WordPress, wp_get_attachment_image() returns a ready-made img tag for a file in the Media Library. You pass the attachment ID and an image size.
It adds the width, height, alt text, and often responsive srcset attributes for you.
Syntax
A few ways to call wp_get_attachment_image().
<?php
echo wp_get_attachment_image( 15, 'medium' );
| Call | What it does |
|---|
wp_get_attachment_image( 15 ) | Use the thumbnail size. |
wp_get_attachment_image( 15, 'medium' ) | Use the medium size. |
wp_get_attachment_image( 15, array( 400, 300 ) ) | Use a size in pixels. |
wp_get_attachment_image( 15, 'medium', false, array( 'class' => 'photo' ) ) | Add your own attributes. |
It returns the HTML. Use echo to print it.
Full declaration
wp_get_attachment_image( int $attachment_id, string|int[] $size = 'thumbnail', bool $icon = false, string|array $attr = '' ): string
Parameters
| Parameter | Description |
|---|
$attachment_id | int. The ID of the image in the Media Library. |
$size | string|int[]. A size name such as thumbnail, medium, large, full, or an array of width and height. Default is thumbnail. |
$icon | bool. true uses a media icon when the file is not an image. Default is false. |
$attr | string|array. Extra attributes for the img tag, such as class or alt. Default is empty. |
Return Value
| Return Value | Description |
|---|
| string | The img tag HTML, or an empty string if the image is not found. |
WordPress Examples
Three short examples. Each one shows the code and what it prints.
Example 1 — Print an image
Print an image by its ID.
<?php echo wp_get_attachment_image( 15, 'medium' ); ?>
<img width="300" height="200" src="https://example.com/wp-content/uploads/2026/11/sunset-300x200.jpg" class="attachment-medium size-medium" alt="Sunset" decoding="async" loading="lazy" />
How It Works
15 is the image ID and medium is the size.- The function returns the whole
img tag. - WordPress may also add
srcset and sizes for different screens.
Example 2 — Add a class and alt text
Use $attr to change attributes.
<?php
echo wp_get_attachment_image(
15,
'medium',
false,
array(
'class' => 'photo',
'alt' => 'A sunset over the sea',
)
);
<img width="300" height="200" src="https://example.com/wp-content/uploads/2026/11/sunset-300x200.jpg" class="photo" alt="A sunset over the sea" decoding="async" loading="lazy" />
How It Works
- The third argument
false means we do not want an icon. - The fourth argument holds extra attributes.
- Our
class and alt replace the defaults.
Example 3 — Show all attached images
Print every image attached to the current post.
<?php
$images = get_attached_media( 'image', get_the_ID() );
foreach ( $images as $image ) {
echo wp_get_attachment_image( $image->ID, 'thumbnail' );
}
<img width="150" height="150" src="https://example.com/wp-content/uploads/2026/11/sunset-150x150.jpg" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" />
<img width="150" height="150" src="https://example.com/wp-content/uploads/2026/11/beach-150x150.jpg" class="attachment-thumbnail size-thumbnail" alt="" decoding="async" loading="lazy" />
How It Works
get_attached_media() returns the image attachments.- Each ID goes into
wp_get_attachment_image(). - The result is a row of thumbnails.
Frequently Asked Questions
It returns an img tag for an attachment ID and an image size.
No. It returns the HTML. Use echo to print it.
Pass a size name such as medium or large, or an array of width and height, as the second argument.
Pass array( 'class' => 'photo' ) as the fourth argument.
Use wp_get_attachment_image_src().
Browse all post functions
Open the functions index for simple guides with examples and FAQs.
Post Functions Index →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful