Skip to content

Instantly share code, notes, and snippets.

@astockwell
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astockwell/c15191c2b041eb337cd3 to your computer and use it in GitHub Desktop.
Save astockwell/c15191c2b041eb337cd3 to your computer and use it in GitHub Desktop.
Better way to get Wordpress post featured image
<?php
/**
* Get everything you could ever want about a featured image
*
* Usage:
* <?php $image = get_featured_image(); ?>
* <img class="thumbnail" src="<?php echo $image['url']; ?>" alt="<?php echo $image['title']; ?>">
*
* @param integer|object $post_id The ID/Object of the post that has the desired post thumbnail image
* @return false|array Null on failure to find the image, wp_prepare_attachment_for_js array on success
*
*/
if (!function_exists('get_featured_image')) {
function get_featured_image($post_id = null) {
if (!$post_id) {
global $post;
$post_id = $post->ID;
if (!$post_id) return false;
}
return wp_prepare_attachment_for_js( get_post_thumbnail_id( $post_id ) );
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment