WordPress wp_get_attachment_image() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Returns an img tag

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().

WordPress
<?php
echo wp_get_attachment_image( 15, 'medium' );
CallWhat 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
WordPress
wp_get_attachment_image( int $attachment_id, string|int[] $size = 'thumbnail', bool $icon = false, string|array $attr = '' ): string

Parameters

ParameterDescription
$attachment_idint. The ID of the image in the Media Library.
$sizestring|int[]. A size name such as thumbnail, medium, large, full, or an array of width and height. Default is thumbnail.
$iconbool. true uses a media icon when the file is not an image. Default is false.
$attrstring|array. Extra attributes for the img tag, such as class or alt. Default is empty.

Return Value

Return ValueDescription
stringThe 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.

WordPress
<?php echo wp_get_attachment_image( 15, 'medium' ); ?>

How It Works

  1. 15 is the image ID and medium is the size.
  2. The function returns the whole img tag.
  3. WordPress may also add srcset and sizes for different screens.

Example 2 — Add a class and alt text

Use $attr to change attributes.

WordPress
<?php
echo wp_get_attachment_image(
  15,
  'medium',
  false,
  array(
    'class' => 'photo',
    'alt'   => 'A sunset over the sea',
  )
);

How It Works

  1. The third argument false means we do not want an icon.
  2. The fourth argument holds extra attributes.
  3. Our class and alt replace the defaults.

Example 3 — Show all attached images

Print every image attached to the current post.

WordPress
<?php
$images = get_attached_media( 'image', get_the_ID() );
foreach ( $images as $image ) {
  echo wp_get_attachment_image( $image->ID, 'thumbnail' );
}

How It Works

  1. get_attached_media() returns the image attachments.
  2. Each ID goes into wp_get_attachment_image().
  3. 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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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