Summaries and Thumbnails on Home Page in Wordpress

Thumbnails

Thumbnails


What we want

A thumbnail image to be displayed along with the summary of each recent article on the ‘home page’ of the site, but a full size image used when the article is displayed on its own single page.

There may be other ways to do this in Wordpress 2.7, but I haven’t found them.
Please feel free to point them out other solutions in the comments.
See also this article at Tutorial9

How to do it

It is not that hard to achieve, but does require a change to the php code within the template you have chosen.

The file to change is the Main Index File, index.php.

Tthe exact place to add this depends on the the template you using.

It goes in the section

1
2
3
4
<div class="entry-content">
...... In here
......
<div class="entry-meta">

The code to paste is like that below -

1
2
3
4
5
6
7
8
9
10
11
12
13
<?php if ($wp_query->post_count>1){
        $thumbnail = get_post_meta($post->ID, "thumbnail", true);
        if ($thumbnail != "") {
            echo "<div class=\"thumbnail\">";
            $values = get_post_custom_values("thumbnail");
            echo "<img  width=\"80px\" src=\"".$values[0]."\" alt=\"".get_the_title()."\">";
            echo "</div>";
        }
        echo the_excerpt(''.__('Read More <span class="meta-nav">&raquo;</span>', 'sandbox').'');
      }else{
        echo the_content(''.__('Read More <span class="meta-nav">&raquo;</span>', 'sandbox').'');
      }    
?>

Then, in any post, you need to add the image that you want to use as the thumbnail to a ‘custom field’.
Add the image to the document in the normal way. The text of your document will then include a line like

1
<img src="http://lemlink.com.au/wp-content/uploads/2009/04/thumb-150x150.jpg" alt="Thumbnails" title="thumb" width="150" height="150" class="size-thumbnail wp-image-18" />>

Copy the src address of the image.
In this case that is

http://lemlink.com.au/wp-content/uploads/2009/04/thumb-150x150.jpg

Paste that into a ‘Custom Field’ down the bottom of the edit post page, as below.

custom_field

That’s it.

I agree it should be simpler.

Similar Posts

One Response to “Summaries and Thumbnails on Home Page in Wordpress”

  1. November 10th, 2009 | 6:05 pm

    Thanks for the nice code. It works perfectly in 2.8.5, thougfh needed a bit twiking.

Leave a reply