Home > Wordpress > WordPress: Get post thumbnail image function

WordPress: Get post thumbnail image function

So I’ve been using the bdw_get_images function (thanks to John Crenshaw and RLM Web Design & Marketing) for a couple of years to generate thumbnails on the index, and sometimes the archives & search results, pages.  Recently I got a call from a client that they had inserted an image into a post but it was not displaying.  Upon further investigation I found that the image that was attached to the post in question had originally been attached to another page.  Since bdw_get_images pulls images by looking for an attachment whose post_parent is the current post and since post_parent is only set for attachments when they are first added, this caused it not to find the image.  I decided it was time I wrote a newer function that would work regardless of whether the image was attached to any other posts.  At some point I think I’ll develop this into a legitimate plug-in with some additional options but here is a basic function you can add to your functions.php file in your theme.


Add the code below to your functions.php file just above the closing “?>“.  Within any of your theme files use the following syntax: “get_post_thumbnail(__POST_ID__)” where __POST_ID__ is the ID of the post.  This outputs a fully formed IMG tag.  If you are using this inside the loop you can just put in “$post->ID” but if you are using it elsewhere you’ll need to get the ID. Here’s the code:

function get_post_thumbnail($iPostID)
  // Get post data
  $post_data = get_post($iPostID);
  // Get post content
  $post_content = $post_data->post_content;
  // See if there is an image attached to the post
  $image_start = strpos($post_content, 'id="attachment_');
  if($image_start) {
    // Get the image ID
    $image_start += 15;
    $image_len = strpos($post_content, '"', $image_start) - $image_start;
    $image_id = substr($post_content, $image_start, $image_len);
    // Get the thumbnail url for the attachment
    $sThumbUrl = wp_get_attachment_thumb_url($image_id);
    // Build the <img> string
    $sImgString = '<img src="' . $sThumbUrl . '" width="90"
                   alt="Thumbnail Image" title="Thumbnail Image" />';
    // Print the image
    echo $sImgString;
  } else {
    // Print a default thumbnail image
    echo '<img src="'. get_bloginfo('stylesheet_directory') .
         '/images/news_defaultthumb.jpg" border="0" alt="Thumbnail Image"
          title="Thumbnail Image" />';

You would need to create a default thumbnail image and by default the function looks for it in the “images” directory of your theme directory and expects it to be named “news_defaultthumb.jpg“. To change these assumptions just alter the “echo …” line at the end of the function.

I hope you find this useful. If you have success using it or run into any problems using it please share them in the comments below!

Categories: Wordpress Tags: , , , , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: