wp_get_attachment_image [ WordPress Function ]
| Parameters: |
|
| Uses: | |
| See: | |
| Returns: |
|
| Defined at: |
|
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.
Source
<?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);
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
'title' => trim(strip_tags( $attachment->post_title )),
);
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 title
$attr = wp_parse_args($attr, $default_attr);
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment );
$attr = array_map( 'esc_attr', $attr );
$html = rtrim("<img $hwstring");
foreach ( $attr as $name => $value ) {
$html .= " $name=" . '"' . $value . '"';
}
$html .= ' />';
}
return $html;
}
?>
Examples [ wp-snippets.com ]
Top Google zoekresultaten
- Function Reference/wp get attachment image « WordPress Codex
<?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 ...
codex.wordpress.org - WordPress › Support » Tags — wp_get_attachment_image
(forgot?) Register · WordPress › Support » wp_get_attachment_image ... Control sort order when using wp_get_attachment_image ? 13, jefffassnacht, 2 months ...
wordpress.org - php - Correct way of using wp_get_attachment_image() in ...
The function wp_get_attachment_image only gets an image that was uploaded to wordpress, it doesn't output an image in the content of the post.
stackoverflow.com - images - use wp_get_attachment_image() to show attachments ...
Mar 8, 2012 ... Making a quick loop to show certain attachment images. lifted it almost straight from the codex but wp_get_attachment_image() isn't behaving ...
wordpress.stackexchange.com
Gebruikersdiscussies [ wordpress.org ]
- waterwalk on "Get Title, Alt, or Caption from Media Library and insert into theme"
- waterwalk on "Get Title, Alt, or Caption from Media Library and insert into theme"
- waterwalk on "Get Title, Alt, or Caption from Media Library and insert into theme"
- jefffassnacht on "Control sort order when using wp_get_attachment_image ?"
- Inmo on "Control sort order when using wp_get_attachment_image ?"
- jefffassnacht on "Control sort order when using wp_get_attachment_image ?"
- Inmo on ""gallery" display order with wp_get_attachment_image"
- Inmo on "Control sort order when using wp_get_attachment_image ?"
- ravichandranjothi on "wp_get_attachment_image - Last image returned from post"
- Kampflebkuchen on "Control sort order when using wp_get_attachment_image ?"