Thesis Tip — How To Display Post Thumbnail On Front Page

By default, the excerpts on blog page or home page does not display the post thumbnails. But, WordPress provides a filter called the_excerpt which can be used to customize the excerpts and thus include post thumbnail as per our requirement.

Post Thumbnail is a theme feature introduced with version 2.9. Thumbnail is an image that is chosen as the representative image for Posts, Pages or Custom Post Types.

Since Post Thumbnail is a theme feature you need to first enable the support for post thumbnail if you are using Thesis. This can be done by simply placing the following code in functions.php:

if ( function_exists( 'add_theme_support' ) )
{
    add_theme_support( 'post-thumbnails' ); 
}

After this, you need to set featured image on your post. This can be done manually on each post or you can WordPress plugin Auto Post Thumbnail that will automatically create a Post Thumbnail (Featured Thumbnail) from the first image in your post. You can also use Regenerate Thumbnails, to regenerate the thumbnails, if you have changed the dimensions of image or want to generate a featured image for the older posts which do not have a post image set.

Now you are all set to use featured image in your theme. Place the below mentioned code in functions.php:

function post_image($t)
{
    if ( has_post_thumbnail( $post->ID ) ) 
    {
        $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail' );
        $t =  '<a href="'.get_permalink().'"><img src="'.$image[0].'" /></a>'.$t; 
    }
    $t = preg_replace('/<p(.*?)>/i', '<p>', $t); 
    return $t; 
}

add_filter('the_excerpt','post_image');

What are you waiting for? Go ahead and try this out!

Posted In:

WordPress Support & Maintenance, Code Snippets, Thesis WordPress Theme Framework

2 Comments

  1. Is there a way to modify this bit of code so it only shows up on the first post on the home page (the non-teaser post) and also show a custom size?

    Reply
  2. Thanks! you have saved my time.

    Reply

Comment on Thesis Tip — How To Display Post Thumbnail On Front Page

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