Ready to get started?Download WordPress


Function Reference/wp get attachment image



Returns an HTML image element representing an attachment file, if there is any, otherwise an empty string.


<?php wp_get_attachment_image$attachment_id$size$icon$attr ); ?>

Default Usage

<?php echo wp_get_attachment_image( 1 ); ?>

If the attachment is an image, the function returns an image at the specified size. For other attachments, the function returns a media icon if the $icon parameter is set to true.

To get attachment IDs dynamically in a template, you can use get_posts('post_type=attachment'), etc.


(integer) (required) ID of the desired attachment.
Default: None
(string/array) (Optional) Image size. Either a string keyword (thumbnail, medium, large or full) or a 2-item array representing width and height in pixels, e.g. array(32,32). As of Version 2.5, this parameter does not affect the size of media icons, which are always shown at their original size.
Default: 'thumbnail'

Instead of using an array which requires checking all of the image sizes, you should consider registering 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.

(boolean) (Optional) Use a media icon to represent the attachment.
Default: 'False'
  • 1 (True)
  • 0 (False) - Default
(string/array) (Optional) Query string or array of attributes.
Default: None
$default_attr = array(
	'src'	=> $src,
	'class'	=> "attachment-$size",
	'alt'   => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )),

The $attr argument is merged with WordPress's default attributes and passed through the wp_get_attachment_image_attributes filter.


Display all images as a list

To display all of the images and titles attached to a certain page and display them as a list of bullets you can use the following:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();    

 $args = array(
   'post_type' => 'attachment',
   'numberposts' => -1,
   'post_status' => null,
   'post_parent' => $post->ID

  $attachments = get_posts( $args );
     if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           echo '<li>';
           echo wp_get_attachment_image( $attachment->ID, 'full' );
           echo '<p>';
           echo apply_filters( 'the_title', $attachment->post_title );
           echo '</p></li>';

 endwhile; endif; ?>

Return Value

an HTML img element or empty string on failure.

Change Log

Since: 2.5.0

Source File

wp_get_attachment_image() is located in wp-includes/media.php.


Attachment Functions: the_attachment_link(), get_attachment_link(), wp_get_attachment_link(), wp_get_attachment_image(), wp_get_attachment_image_src(), wp_get_attachment_url(), wp_get_attachment_thumb_file(), wp_get_attachment_thumb_url(), is_attachment(), wp_get_attachment_metadata()

See also index of Function Reference and index of Template Tags.