Support » Fixing WordPress » How do I use custom fields to insert a thumbnail in my excerpt?

  • Resolved pinkbeats


    Following on from this thread about an issue I had with excerpts which I managed to resolve myself, I’m now trying to work out how to insert a thumbnail image into my excerpts to liven them up.

    I would like to use the thumbnail image that WordPress automatically generates and saves whenever you upload one as it’s about the right size at 150×150 px and saves me uploading one or saves the browser from scaling down the full size image contained within the post. Some plugins do generate thumbnails from images contained with the post but not all of my posts have images that are suitable for thumbnails, e.g. just contain an RSS icon, which looks rubbish when scaled up to 150×150 px.

    I’m wondering if there is a way to force a thumbnail image to show in my excerpts by using custom fields as I can then control when I want one to display and when I don’t. Thanks in advance for any pointers.

Viewing 4 replies - 1 through 4 (of 4 total)
  • esmi


    Forum Moderator

    Thanks – so from the link you have posted, WordPress 2.9 supports post thumbnails however, the theme I am currently using doesn’t. I experimented after reading Mark Jaquith’s article by adding add_theme_support( 'post-thumbnails', array( 'post' ) ); to the functions.php file for the theme. Whilst it did enable the thumbnail functionality, it also caused a header error that crashed the admin.

    Now it’s very likely that I didn’t implement something correctly, I’m unsure of what the correct syntax I should be using to add add_theme_support( 'post-thumbnails', array( 'post' ) ); to the functions.php file.

    Should it be:

    add_theme_support( 'post-thumbnails', array( 'post' ) );

    In the meantime, I got a thumbnail to display with the excerpt by using the suggestions from David Leggett’s Add Thumbnails to WordPress with Custom Fields tutorial, adding this <?php $Thumbnail = get_post_meta($post->ID, 'thumbnail', $single = true); ?> right after the loop starts in the index.php file and

    <?php if($Thumbnail !== '') { ?>
      		<img src="<?php echo $Thumbnail;?>" />
      		<?php } ?>

    further down within the loop.

    However, I’ve decided the thumbnail is too large plus I need to work out how to control it’s positioning in relation to the excerpt text.

    From what I’ve read on using the_post_thumbnail, sounds preferable as it negates the need to use the custom field which sounds like it’s been depreciated by the introduction of the_post_thumbnail in WordPress 2.9 – just currently can’t get it to work.

    Resolved, but not by using custom fields as originally asked in the title of this thread. As previously indicated, such use is depreciated since the introduction of the_post_thumbnail feature that came with WordPress 2.9 – so to make it work after digesting Mark Jaquith’s article, I also found it useful to read the article on by Ed.

    Added to my theme’s index.php:

    <?php if ( function_exists( 'add_theme_support' ) ) : ?>
    		  <?php if ( has_post_thumbnail() ) : ?>
    			<div class="postthumb">
    			<?php the_post_thumbnail( 'hp-post-thumbnail' ); ?>
     			<?php endif; ?>
                <?php endif; ?>

    Added to my theme’s functions.php:

    /* switch on support for post thumbnails - WordPress 2.9 onwards */
    if ( function_exists( 'add_theme_support' ) ) { // Added in 2.9
     add_theme_support( 'post-thumbnails' );
     set_post_thumbnail_size( 110, 110, true); // Normal post thumbnails
     add_image_size( 'hp-post-thumbnail', 110, 110, true); // Homepage thumbnail size
     add_image_size( 'single-post-thumbnail', 330, 9999 ); // Permalink thumbnail size

    And that’s it – now I’ve just got to write manual extracts and create thumbnails for all my posts… a work in progress!

    One thing I didn’t understand was that even though I was adding php code to functions.php, my understanding is that you shouldn’t need to explicitly wrap any code in php tags (<?php ?>) but if I didn’t then the code didn’t work. A minor point but one I’d be interested to know the answer to nonetheless.

    One other thing – I didn’t bother to add the code to the single.php file as I have no need for thumbnails to display in my posts when viewed by themselves as they have full size images within them.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How do I use custom fields to insert a thumbnail in my excerpt?’ is closed to new replies.