Skip to content

Instantly share code, notes, and snippets.

@iamcanadian1973
Last active August 29, 2015 14:02
Show Gist options
  • Save iamcanadian1973/32c57bcc28b112a8b62e to your computer and use it in GitHub Desktop.
Save iamcanadian1973/32c57bcc28b112a8b62e to your computer and use it in GitHub Desktop.
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);
?>
@colinhowells
Copy link

groovy, this is terrific - $image_atr should be $image tho

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment