wp_get_attachment_image( int $attachment_id, string|array $size = 'thumbnail', bool $icon = false, string|array $attr = '' )

Get an HTML img element representing an image attachment


Description Description

While $size will accept an array, it is better to register 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 and then having the browser scale down the image.


Parameters Parameters

$attachment_id

(int) (Required) 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 value: 'thumbnail'

$icon

(bool) (Optional) Whether the image should be treated as an icon.

Default value: false

$attr

(string|array) (Optional) Attributes for the image markup.

Default value: ''


Top ↑

Return Return

(string) HTML img element or empty string on failure.


Top ↑

Source Source

File: wp-includes/media.php

function wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' ) {
	$html  = '';
	$image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
	if ( $image ) {
		list($src, $width, $height) = $image;
		$hwstring                   = image_hwstring( $width, $height );
		$size_class                 = $size;
		if ( is_array( $size_class ) ) {
			$size_class = join( 'x', $size_class );
		}
		$attachment   = get_post( $attachment_id );
		$default_attr = array(
			'src'   => $src,
			'class' => "attachment-$size_class size-$size_class",
			'alt'   => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),
		);

		$attr = wp_parse_args( $attr, $default_attr );

		// Generate 'srcset' and 'sizes' if not already present.
		if ( empty( $attr['srcset'] ) ) {
			$image_meta = wp_get_attachment_metadata( $attachment_id );

			if ( is_array( $image_meta ) ) {
				$size_array = array( absint( $width ), absint( $height ) );
				$srcset     = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id );
				$sizes      = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id );

				if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
					$attr['srcset'] = $srcset;

					if ( empty( $attr['sizes'] ) ) {
						$attr['sizes'] = $sizes;
					}
				}
			}
		}

		/**
		 * Filters the list of attachment image attributes.
		 *
		 * @since 2.8.0
		 *
		 * @param array        $attr       Attributes for the image markup.
		 * @param WP_Post      $attachment Image attachment post.
		 * @param string|array $size       Requested size. Image size or array of width and height values
		 *                                 (in that order). Default 'thumbnail'.
		 */
		$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
		$attr = array_map( 'esc_attr', $attr );
		$html = rtrim( "<img $hwstring" );
		foreach ( $attr as $name => $value ) {
			$html .= " $name=" . '"' . $value . '"';
		}
		$html .= ' />';
	}

	return $html;
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.5.0 Introduced.

Top ↑

More Information More Information

Top ↑

Usage Usage

wp_get_attachment_image( $attachment_id, $size, $icon, $attr );

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( array( 'post_type' => 'attachment' ) ), etc.



Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Rinku Y

    wp_get_attachment_image function can accept four values as you can see:

    wp_get_attachment_image ( int $attachment_id, string|array $size = 'thumbnail', bool $icon = false, string|array $attr = '' )

    So i always use:

    <?php echo wp_get_attachment_image( get_the_ID(), array('700', '600'), "", array( "class" => "img-responsive" ) );  ?>

    Note: we can simply use get_the_ID() to pass id of active post. and here 700 is width and 600 is height of attachment image. we can also pass our class as array( “class” => “img-responsive” ).

  2. Skip to note 2 content
    Contributed by tazotodua

    default call returns like this:

    <img width="150" height="150" src="http://example.com/wp-content/uploads/2017/11/image-xyz-150x150.jpg" class="attachment-thumbnail size-thumbnail" alt="" srcset="http://example.com/wp-content/uploads/2017/11/image-xyz-150x150.jpg 150w, http://example.com/wp-content/uploads/2017/11/image-xyz-50x50.jpg 50w" sizes="(max-width: 150px) 100vw, 150px" />
    
  3. Skip to note 3 content
    Contributed by Codex

    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:

    <ul>
    	<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
    
    		$attachments = get_posts( array(
    			'post_type'   => 'attachment',
    			'numberposts' => -1,
    			'post_status' => null,
    			'post_parent' => $post->ID
    		) );
    		
    		if ( $attachments ) {
    			foreach ( $attachments as $attachment ) {
    				?>
    				<li><?php echo wp_get_attachment_image( $attachment->ID, 'full' ); ?>
    					<p><?php echo apply_filters( 'the_title', $attachment->post_title ); ?></p>
    				</li>
    				<?php
    			}
    		}
    	endwhile; endif; ?>
    </ul>
    

You must log in before being able to contribute a note or feedback.