How to get the first image thumbnail from the wordpress post using wordpress

Sponsors

Most WordPress theme are using custom fields to display thumbs on their blog homepage.

But you can take easily extract the first image from the post, and display the image as post thumbnail.

Just copy paste the following function in your functions.php file.


function get_first_image($id) {
$PostID = $id;
$all_images =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $PostID );

if($all_images) {

$arr_of_all_images = array_keys($all_images);

// Get the first image attachment from post
$firstImage = $arr_of_all_images[0];

// Get the thumbnail url for the attachment
// If you want the full-size image instead of the thumbnail, use wp_get_attachment_url() instead of wp_get_attachment_thumb_url().
$thumb_url = wp_get_attachment_thumb_url($firstImage);

// Build the <img> string
$First_thumb_image = '<a href="' . get_permalink() . '">' .
'<img src="' . $thumb_url . '" width="150" height="150" alt="Thumbnail Image" title="Thumbnail Image" />' .
'</a>';

// Print the thum image form post
echo $First_thumb_image;
}
}

You can call above “get_first_image” function anywhere in your post loop. I loop you need to just copy paste the following lines
to print the first image of post body.

<?php get_first_image($post->ID);  ?>

wordpress
Follwing article I found useful

http://codex.wordpress.org/Plugin_API/Filter_Reference

http://codex.wordpress.org/Function_Reference/wp_get_attachment_thumb_url

Incoming search terms:

You may like following Articles!

This entry was posted in wordpress, wordpress api and tagged by Wordpress API. Bookmark the permalink.

About Wordpress API

API means Application Program interface. In Wordpress API we are giving all the information about wordpress API. We worked for many wordpress plugins and themes. We have expertise in PHP, Databases, Graphic Design, Ruby on Rails, Java, Wordpress, Drupal and many CMS. We always give you the best solutions about wordpress and other related topics in This Blog. We will give you always fresh tips and tricks and techniques about wordpress in this blog.

5 thoughts on “How to get the first image thumbnail from the wordpress post using wordpress

  1. Hi it is great function to get first image from post but please tell me how can i resize first image with timthumb.php and i want different sizes in one loop for lightbox and post thumbnail

  2. Thanks for this post, i’m unable to get it working though. After trying several tutorials on using the first post image for thumbnail. This one just doesn’t display anything at all, no errors, but no image.
    I added the line to the loop, no idea why it’s not showing anything though >.<

  3. I’m unable to get this to display the thumbnail at all, I’m sure I’ve added it properly. I’ve added thumbnail support and after doing that it displays the same image on every post. Any suggestions?

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>