X

Return the first image attachment from the post as thumbnail

Get the first image from post and diplay it in any size.

Code Snippet:

<?php

function first_img($size="thumbnail") {
  $files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image');
  if($files) :
    $keys = array_reverse(array_keys($files));
    $num = $keys&#91;0&#93;;
    $image_attributes = wp_get_attachment_image_src( $num, $size );
    echo '<img src="'.$image_attributes&#91;0&#93;.'" width="'.$image_attributes&#91;1&#93;.'" height="'.$image_attributes&#91;2&#93;.'" class="firstimg" />';
  endif;
}

?>

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!