iamcanadian1973
6/11/2014 - 5:52 PM

Get image attachment (title, alt, caption, url, width, height)

Get image attachment (title, alt, caption, url, width, height)

/**
 * Get image attachment (title, alt, caption, url, width, height)
 *
 */

// get attachment ID if featured image

$attachment_id = get_post_thumbnail_id( $post->ID );

// best function ever - http://codex.wordpress.org/Function_Reference/wp_prepare_attachment_for_js

$image = wp_prepare_attachment_for_js($attachment_id);

// if using a custom image size then make sure it exists if not default to "full"

$size = isset($image['sizes']['custom-size']) ? 'custom-size' : 'full';
$title = $image['title'];
$alt = $image_atr['alt'];
$caption = $image_atr['caption'];
$url = $image['sizes'][$size]['url'];
$width = $image['sizes'][$size]['width'];
$height = $image['sizes'][$size]['height'];

// print out the image

if ($image) printf('<img src="%s" alt="%s" width="%s" height="%s" />', $url, $alt, $width, $height);
?>