Get an HTML img element representing an image attachment
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.
Signature
wp_get_attachment_image( $attachment_id, $size = 'thumbnail', $icon = false, $attr = '' )
- attachment_id
- (int) Image attachment ID.
- size
- (string) Optional, default is 'thumbnail'.
Default:'thumbnail' - icon
- (boolean) Optional, default is false. Whether it is an icon.
Default:false - attr
-
Default:''
Return
(string) HTML img element or empty string on failure.
Source
WP Trac GitHub Bitbucketfunction 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); if ( is_array($size) ) $size = join('x', $size); $attachment = get_post($attachment_id); $default_attr = array( 'src' => $src, 'class' => "attachment-$size", 'alt' => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first ); if ( empty($default_attr['alt']) ) $default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption if ( empty($default_attr['alt']) ) $default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title12 more lines...
Link here
-
URL
http://queryposts.com/function/wp_get_attachment_image/ -
HTML
<a href='http://queryposts.com/function/wp_get_attachment_image/'>wp_get_attachment_image()</a> -
Markdown
[wp_get_attachment_image()](http://queryposts.com/function/wp_get_attachment_image/) -
BBCode
[url=http://queryposts.com/function/wp_get_attachment_image/]wp_get_attachment_image()[/url]