Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using timthumb as the image size management script throughout my wordpress site but I just noticed that an empty image tag shows up even when there is no thumbnail.

<?php if(has_post_thumbnail()):
    $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
?>
    <div class="wp-caption alignright">
        <img src="<?php bloginfo('template_url'); ?>/scripts/timthumb.php?src=<?php echo $src[0]; ?>&w=145&h=80&q=100&zc=1" height="80" width="145" alt="<?php the_title(); ?>" class="wp-post-image" />
        <p class="wp-caption-text"><?php the_post_thumbnail_caption(); ?></p>
    </div>
<?php endif; ?>

I am doing it this way because I wanna show the caption whenever it exists. I also tried doing it all in php. I never do it like this, so I'm not sure if it's correct but It also shows the same thing and also echos the template_url before the image.

<?php if(has_post_thumbnail()){
    $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full');
    echo '<div class="wp-caption alignright">
        <img src="'. bloginfo('template_url') .'/scripts/timthumb.php?src='. $src[0] .'&w=145&h=80&q=100&zc=1" height="80" width="145" alt="'. the_title() .'" class="wp-post-image" />
        <p class="wp-caption-text">'. the_post_thumbnail_caption() .'</p>
    </div>';
} ?>

Am I doing something wrong here. That img tag is inside the condition right? Still a broken image place holder is showing up with every post. Image below

broken image placeholder

Can anyone tell me what's wrong with the code.

Thanks!

I appreciate all the help.

share|improve this question
1  
Were did you use your code? Directly in the single and loop template files? I've (more or less) the same code as u and its working as expected. Offtopic: You could use add_image_size('handler', 100, 50) to register your desired image sizes and set the same handler in wp_get_attachment_image_src($postId, 'handler') to display it. No need for timthumb here. –  rofflox Aug 23 '11 at 11:23
    
What Roman said. There is absolutely no need to use TimThumb in this context. (@Roman you should make that an answer, to explain how to use the core functionality in order to accomplish what the OP wants.) –  Chip Bennett Aug 23 '11 at 12:16
    
I know about the core functions but it doesn't absolutely crop the image the way I want. I always want width 145 and height 80. –  Aayush Aug 23 '11 at 12:28
    
Oh and the main thing, I forgot: I've got the first code in my index loop file. –  Aayush Aug 23 '11 at 12:30
    
@Aayush add_image_size has also crop-functionality. Set the last parameter to true will force to crop the image. However, check the answer below. On my local install the thumbnail is only displayed if a featured image is set. –  rofflox Aug 23 '11 at 12:43
add comment

1 Answer 1

up vote 0 down vote accepted

As commented above, you don't have to use TimThumb at all to get additional image sizes for your uploaded images.

WordPress ships with the add_image_size() function.

// functions.php
add_action('init', 'wpse26655_add_additional_image_sizes');
function wpse26655_add_additional_image_sizes() {
    add_image_size('thumb200x120', 200, 120, true);
}

In your template files, you could than query your thumbnail with your desired image size. E.g.

<?php
// single.php / loop-content.php

$thumbId = get_post_thumbnail_id();

if($thumbId) :
    $thumbSrc =  wp_get_attachment_image_src($thumbId , 'thumb200x120');
    $thumbUrl = $thumbSrc[0];
    $thumbWidth = $thumbSrc[1];
    $thumbHeight = $thumbSrc[2];
?>

<div class="post-thumbnail">
    <img src="<?php echo $thumbUrl; ?>" width="<?php echo $thumbWidth; ?>" height="<?php echo $thumbHeight; ?>" />
</div>

<?php endif; ?>

Note:

The additional images sizes are generated on image upload. If you want new sizes for existing images inside your medialibray, you have to recreate all thumbnail images. A very good plugin for this purpose is Regenerate Thumbnails.

share|improve this answer
    
Timthumb officially gone. Thanks for that. A lot cleaner code now, but the root problem remains. Still empty img tags. –  Aayush Aug 23 '11 at 14:53
    
Question answered. It was imported content from my online site without the imported media. Sorry for wasting your time. I did learn stuff though. Thanks! –  Aayush Aug 23 '11 at 14:58
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.