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.

currently I m showing post image and it's exceprt on my index page of website. Beacuse of this site is slow downs as each image is passed to timthumb.php file. I want to change in code so that image is displayed direct without passing to timthumb.php file for resizing. Is there any way to achieve this??

Thanks in advance..

share|improve this question
1  
Show the code from you index.php template. –  Chris Carson Aug 8 '11 at 13:16
    
Is the image in question an attached image? Is the image in question the Post Featured Image? –  Chip Bennett Aug 8 '11 at 13:19
add comment

4 Answers

add the size to your images sizes using add_image_size() to your theme's functions.php file. eg:

if ( function_exists( 'add_theme_support' ) ) {
    add_image_size( 'index-thumb', 220, 180 ); // 220 pixels wide by 180 pixels tall
}

then set the image as featured image from the media uploader of the posts :

enter image description here

then open your index.php and replace the image tag that is calling the timthumb.php file for resizing with:

if(has_post_thumbnail()) {
    the_post_thumbnail('index-thumb');
} else {
    //show default image if none is set
    echo '<img src="'.get_bloginfo("template_url").'/images/img-default.png" />';
}
share|improve this answer
    
Thanks for reply. Can we set image size setting in media setting instead of adding add_image_size() in function.php file.?? –  user392406 Aug 11 '11 at 12:28
    
Not really since the add_image_size has to be fired before the image is uploaded. So out of the box it won't work but with some custom coding you can do that. –  Bainternet Aug 11 '11 at 14:05
add comment

You can use the following function to get the image attached to the post:

function firstimg($parent){
    global $wpdb;
    $image = wpdb->get_results('SELECT * FROM '.$wpdb->posts.' WHERE post_parent='.$parent.' AND post_type="attachment" AND post_mime_type LIKE "%image%" ORDER BY menu_order ASC LIMIT 1');

    return $image;
}
share|improve this answer
    
I much prefer get_children(). –  Chip Bennett Aug 8 '11 at 14:59
add comment

Check out this article I wrote on images, image sizes and thumbnails in WordPress: http://theme.fm/2011/08/images-image-sizes-and-post-thumbnails-in-wordpress-1385/

share|improve this answer
add comment

I see someone else has already recommended using the_post_thumbnail(), but I wanted to point out that you can use this function dynamically to create images of any size.

In your code, just use:

the_post_thumbnail( array(100, 150) );

This will display the post's thumbnail image at 100px wide by 150px high.

If your image isn't set as the post's thumbnail, you can use wp_get_attachment_image() in a similar fashion:

echo wp_get_attachment_image( 1, array(100, 150) );

Just substitute the ID of the attached image for the 1 above, and the width/height you want. Be sure to use echo because the function by default "gets" the image information and returns it ... you need to echo it back to the browser.

share|improve this answer
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.