Enhancing Thumbnail for Excerpts plugin

With WordPress 3.0 a feature called Featured Image was introduced. It enables the poster to add a thumbnail to his post. But if you have written lots of blog entries with an older version of WordPress and you still want to display thumbnails on them, you’ll have to find sine other solution, if you don’t want to add thumbnails to all of them.

You can, of course, use a freely available plugin such as Thumbnail for Excerpts. But it has one flaw. It only accepts/generates thumbnails with static width and height. If you have a variable lengths of picture’s axises (let’s say 100×100, 75×100, 100×75) the plugin will detect only the boxed (and cropped one).

To get around this – to use the excerpt and Feature Image – the plugin (the example refers to version 2.1) has to be corrected a bit.

Add function into “thumbnailforexcerpt.php”:

function tfe_get_attached_image($id,$align) {
  $images = get_children("post_parent=$id&post_type=attachment
    &post_mime_type=image");
  if($images) {
    $image = min(array_keys($images));
    $src = wp_get_attachment_thumb_url($image);
    $alt = get_post_meta($image,'_wp_attachment_image_alt',true);
    $titleA = get_post($image, ARRAY_A);
    $title = $titleA['post_title'];
    return '<img width="'.$width.'" src="'.$src.'
      " alt="'.$alt.'" title="'.$title.'" />';
  }
}

And in the first line of the function “tfe_get_image”, write “return tfe_get_attached_image($id,$align);”.

What does the code do? It finds all images, attached to the post through Media Library. Then it reads the thumbnail for the first attached image and extracts the ALT and TITLE tag. The size of the thumbnail is stored alongside the image, so there is no need to guess it’s size.

Leave a Comment





NOTE - You can 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>