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'm trying to figure how to scale the_post_thumbnail to a specific width? Can you guys help?

Currently I'm using

<?php
        //Resize post thumbnail
        $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'single-post-thumbnail' );
        $thumbnailHeight = $thumbnail[1];
        $thumbnailWidth = $thumbnail[2];
        if($thumbnailHeight != 0){
            $ratio = $thumbnailWidth/$thumbnailHeight;
            $width = 220;
            $height = $width * $ratio;
        }else{
            $height = $thumbnailHeight;
            $width = $thumbnailWidth;
        }
    ?>

And printing the thumbnail with

 <img src="<?php echo $thumbnail[0]; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>"/>

It works but....

Thanks!

share|improve this question
    
What you are asking here ? –  amit Aug 4 '12 at 20:24
    
How to use this function codex.wordpress.org/Function_Reference/the_post_thumbnail to display a scaled thumbnail! Regards –  Joao Ismael Aug 4 '12 at 20:30
    
I want to scale the image, based on a specific width 220px. I don't know the height! –  Joao Ismael Aug 4 '12 at 20:39
    
@amit please don't post answers as comments. –  Chip Bennett Aug 4 '12 at 21:10
    
Joao, if my answer below worked out for you, could you please accept it as correct for future users. Thanks! –  FlashingCursor Aug 9 '12 at 3:59
add comment

3 Answers

up vote 2 down vote accepted

This should work:

You could use:

add_image_size( 'category-thumb', 220, 9999 ); //220 pixels wide (and unlimited height)

and then to display:

if ( has_post_thumbnail() ) { the_post_thumbnail( 'category-thumb' ); }

This will create a thumbnail size of 220 by "unlimited"... Set the featured image on the post, then use the display code in your theme to show the 220x??? scaled image.

share|improve this answer
    
Bingo! Note also that, as far as I know, passing an array to the_post_thumbnail() still doesn't work. Adding an image size, and then calling it via the_post_thumbnail() is the best and, really, only way to go. –  Chip Bennett Aug 4 '12 at 21:09
    
Worked perfectly! (sorry on vacation :) THANKS –  Joao Ismael Aug 17 '12 at 13:09
add comment

I just wanted to mention timthumb aswell. you can do anything you want with this.

all info
http://code.google.com/p/timthumb/
or
http://www.binarymoon.co.uk/projects/timthumb/

share|improve this answer
add comment

Pretty new to Wordpress and PHP, so have been cutting and crow-baring code together at the moment. This might be of some help so i thought I would post, basically I have a slider on my front page with a custom thumbnail size, and also a custom thumbnail size on my blog roll page. I also needed to link the images on my blog roll page to the post.

First I created I added theme support for the thumbnails in functions.php

<?php   /* CUSTOM POST THUMBNAILS */
if ( function_exists( 'add_theme_support' ) ) {
add_theme_support('post-thumbnails');
add_image_size('nivothumb', 960, 320, true);
add_image_size('postthumb', 690, 230, true);
}
?>

Then for the nivoslider I used the below code, on my front_page.php along with some loop code.

<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('nivothumb'); ?></a>

Then for the blog roll thumbnail image I used the following, on my index.php along with the some loop code.

<?php if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail('postthumb');?></a>
<?php endif; ?>

Not sure if this is of some help and if this is not the best way to do it I would be interested to know.

Cheers,

Ant

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.