Get Featured image WordPress

Get Featured image WordPress

Nice little function to add to your themes function.php file to return the featured image of a WordPress page or post.

function get_feat_image($postID, $imageSize, $returnType)
{
$attachmentID = get_post_thumbnail_id($postID);
if ($attachmentID) {
$image_attributes = wp_get_attachment_image_src($attachmentID, $imageSize) ? wp_get_attachment_image_src($attachmentID, $imageSize) : wp_get_attachment_image_src($attachmentID, 'full');
$imageUrl = wp_get_attachment_image_src($attachmentID, $imageSize);
if ($returnType == 'tag') {
//return tag
return '<img class="img" alt="" src="' . $imageUrl[0] . '" /> ';
} else {
//return url
return $imageUrl[0];
}
}
}

To use it just call the function from your templates:
get_feat_image($postID,$imageSize,'tag')

$postID = Post ID you want the images from
$imageSize = the named size of the image
‘tag’ = optional this will return the full tag