wp_get_attachment_image

Get an HTML img element representing an image attachment.

Description

(string) wp_get_attachment_image( (int) $attachment_id, (string) $size = 'thumbnail', (constant) $icon = false, (string) $attr = '' ); 

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.

Returns (string)

HTML img element or empty string on failure.

Parameters (4)

0. $attachment_id (int)
Image attachment ID.
1. $size — Optional. (string) => 'thumbnail'
Image size. Accepts any valid image size, or an array of width and height values in pixels (in that order). Default thumbnail..
2. $icon — Optional. (constant) => false
Whether the image should be treated as an icon. Default false.
3. $attr — Optional. (string) => ''
Attributes for the image markup. Default empty.

Usage

  1. if ( !function_exists( 'wp_get_attachment_image' ) ) { 
  2. require_once ABSPATH . WPINC . '/media.php'; 
  3.  
  4. // Image attachment ID. 
  5. $attachment_id = -1; 
  6.  
  7. // Optional. Image size. Accepts any valid image size, or an array of width 
  8. // and height values in pixels (in that order). Default 'thumbnail'. 
  9. $size = 'thumbnail'; 
  10.  
  11. // Optional. Whether the image should be treated as an icon. Default false. 
  12. $icon = false; 
  13.  
  14. // Optional. Attributes for the image markup. Default empty. 
  15. $attr = ''; 
  16.  
  17. // NOTICE! Understand what this does before running. 
  18. $result = wp_get_attachment_image($attachment_id, $size, $icon, $attr); 
  19.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/media.php  
  1. function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') { 
  2. $html = ''; 
  3. $image = wp_get_attachment_image_src($attachment_id, $size, $icon); 
  4. if ( $image ) { 
  5. list($src, $width, $height) = $image; 
  6. $hwstring = image_hwstring($width, $height); 
  7. $size_class = $size; 
  8. if ( is_array( $size_class ) ) { 
  9. $size_class = join( 'x', $size_class ); 
  10. $attachment = get_post($attachment_id); 
  11. $default_attr = array( 
  12. 'src' => $src,  
  13. 'class' => "attachment-$size_class size-$size_class",  
  14. 'alt' => trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) ),  
  15. ); 
  16.  
  17. $attr = wp_parse_args( $attr, $default_attr ); 
  18.  
  19. // Generate 'srcset' and 'sizes' if not already present. 
  20. if ( empty( $attr['srcset'] ) ) { 
  21. $image_meta = wp_get_attachment_metadata( $attachment_id ); 
  22.  
  23. if ( is_array( $image_meta ) ) { 
  24. $size_array = array( absint( $width ), absint( $height ) ); 
  25. $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id ); 
  26. $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id ); 
  27.  
  28. if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) { 
  29. $attr['srcset'] = $srcset; 
  30.  
  31. if ( empty( $attr['sizes'] ) ) { 
  32. $attr['sizes'] = $sizes; 
  33.  
  34. /** 
  35. * Filters the list of attachment image attributes. 
  36. * @since 2.8.0 
  37. * @param array $attr Attributes for the image markup. 
  38. * @param WP_Post $attachment Image attachment post. 
  39. * @param string|array $size Requested size. Image size or array of width and height values 
  40. * (in that order). Default 'thumbnail'. 
  41. */ 
  42. $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size ); 
  43. $attr = array_map( 'esc_attr', $attr ); 
  44. $html = rtrim("<img $hwstring"); 
  45. foreach ( $attr as $name => $value ) { 
  46. $html .= " $name=" . '"' . $value . '"'; 
  47. $html .= ' />'; 
  48.  
  49. return $html;