7
votes

I am trying to set the width of a post thumbnail using:

the_post_thumbnail()

The width of the image needs to be 210px but the height is not meant to be fixed as all the images will be different sizes. I have tried:

the_post_thumbnail( array( 210, 0 ) ) 

But this does not work. Any ideas?

Thanks.

4

4 Answers

23
votes

Add this inside your functions.php

if ( function_exists( 'add_image_size' ) ) { 
    add_image_size( 'category-thumb', 210, 9999 ); //210 pixels wide (and unlimited height)

}

Use it inside your theme's template files

<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'category-thumb' ); } ?>

For default thumbnail add this inside your functions.php

if ( function_exists( 'add_theme_support' ) ) {
    add_theme_support( 'post-thumbnails' );
    set_post_thumbnail_size( 150, 9999 ); // default Post Thumbnail dimensions   
}

Reference: Here.

2
votes

Change the_post_thumbnail(array(210,0)) to the_post_thumbnail(array(210,9999))

-1
votes

Open your themes functions.php file and add your image size:

Example:

add_image_size('slider', 525, 339, true);

Breakdown of the code:

  • 'slider'= The name of the new image size (use this when adding the new image size to your template file).
  • 525 = The image width
  • 339 = The image height
  • true = The image will be cropped to the exact dimensions listed.

Usage: Use a current template (index.php, single.php, etc.), or create your own. Place this inside the loop (usually just after the_title();) :

the_post_thumbnail('slider');

Now when you add a page or post that uses the template you added the code above, use the featured image option to upload an image. It will output to the size you've created.

Note: This will only apply to newly uploaded images.

-2
votes

Did you try this : Thumbnail Custom Size

It is the best way to change Thumbnail Size.