WordPress.org

Ready to get started?Download WordPress

Forums

How can I use the featured image with timthumb? (3 posts)

  1. Chris
    Member
    Posted 1 year ago #

    I have a theme and the theme using timthumb to re-size the images, the theme use the last image for each post, I want to display the featured image for the post.

    The code in the function.php
    --------------------------------------------------

    //Get image attachment (sizes: thumbnail, medium, full)
    
    function get_thumbnail($postid=0, $size='full') {
    	if ($postid<1)
    	$postid = get_the_ID();
    	$thumb_key = get_theme_mod('thumb_key');
    	if($thumb_key)
    		$thumb_key = $thumb_key;
    	else
    		$thumb_key = 'thumb';
    	$thumb = get_post_meta($postid, $thumb_key, TRUE); // Declare the custom field for the image
    	if ($thumb != null or $thumb != '') {
    		return $thumb;
    	} elseif ($images = get_children(array(
    		'post_parent' => $postid,
    		'post_type' => 'attachment',
    		'numberposts' => '1',
    		'post_mime_type' => 'image', ))) {
    		foreach($images as $image) {
    			$thumbnail=wp_get_attachment_image_src($image->ID, $size);
    			return $thumbnail[0];
    		}
    	} else {
    		return get_bloginfo ( 'stylesheet_directory' ).'/images/default_image.png';
    	}
    
    }
    
    // Automatically display thumbnail
    function post_thumbnail($width, $height) {
    	echo '<a href="'.get_permalink($post->ID).'" rel="bookmark"><img src="'.get_bloginfo('template_url').'/thumb.php?src='.get_thumbnail($post->ID, 'full').'&h='.$height.'&w='.$width.'&zc=1" alt="'.get_the_title().'"/></a>';
    }

    --------------------------------------------------

    The code in the loop:
    --------------------------------------------------
    <?php post_thumbnail(135, 135);?>
    --------------------------------------------------

    The question is how to change the images to Featured images?

    Thanks!

  2. Chris
    Member
    Posted 1 year ago #

    Solved:

    //Get image attachment (sizes: thumbnail, medium, full)
    
    function get_thumbnail($postid=0, $size='full') {
    	if ($postid<1)
    	$postid = get_the_ID();
    	$thumb = get_post_meta($postid, "thumb", TRUE); // Declare the custom field for the image
    	if ($thumb != null or $thumb != '') {
    		echo $thumb;
    	} elseif ($images = get_children(array(
    		'post_parent' => $postid,
    		'post_type' => 'attachment',
    		'numberposts' => '1',
    		'post_mime_type' => 'image', ))) {
    		foreach($images as $image) {
    			$image_id = get_post_thumbnail_id();
    			$thumbnail=wp_get_attachment_image_src($image_id, 'full');
    			return $thumbnail[0];
    		}
    	} else {
    		return get_bloginfo ( 'stylesheet_directory' ).'/images/default_image.png';
    	}
    
    }
  3. Chris
    Member
    Posted 1 year ago #

    But the if there is no featured image, the default image can't work....

    What's wrong with this?

Topic Closed

This topic has been closed to new replies.

About this Topic