Support » Fixing WordPress » the_post_thumbnail image sizes

  • Resolved miocene22


    I’m using the the_post_thumbnail function in my theme to generate thumbnails for a scrolling image slider.

    I want the images to be sized to 260px width and 190px height so I have put the_post_thumbnail(array(260,190)); in the theme.

    These kind of works but some images resized but their proportions are maintained. This means I might get an image that gets resized to 260px width but its height is only 150px. This messes up the look of the slider so I want the function to crop some images so they are always exactly 260×160.

    As I don’t know what proportion the images that may be uploaded in the future the function must be able to deal with this. Is it possible?

Viewing 3 replies - 1 through 3 (of 3 total)
  • any ideas anyone?

    OK, I’ve managed to work it out myself.

    If anyone’s interested here’s how it works:

    In functions.php add this function under the add_theme_support( 'post-thumbnails' ); function:

    add_image_size( 'mycustomsize', 260, 190, true );

    This registers a new image size to be defined for post thumbnails. The ‘true’ in the function tells wordpress to crop images if necessary to achieve the desired proportions. This means your images will be made to be exactly the dimensions specified, not just width/height limited.

    Then just call the custom size in your theme by using the image size string in the function:

    <?php the_post_thumbnail('mycustomsize'); ?>

    Obviously, you can add more custom sizes and call them when required throughout the theme.

    Note: Image cropping is only applied to newly uploaded images; images previously uploaded as post thumbnails will not be cropped after adding the image size function, however they will be resized as normal.

    Hope this helps some people



    Thank you for this infomation, i added:

    add_image_size( 'mycustomsize', 80, 80, true );

    to my functions.php and included it in the head area with:

    echo '<meta property="text" content="' . the_post_thumbnail('mycustomsize') . '" />';

    and it outputted the image at the top of the page.

    1. can u tell me how i would output the url of this custom sized thumb?
    2. it seemed to resize at 80 x 41 – is there anyway to define it exactly 80 x 80?
    (edit – ah see you answered this, existing images will not be cropped)


    ps i’ve been using:

    echo '<meta property="text" content="' . wp_get_attachment_thumb_url( get_post_thumbnail_id( $post->ID ) ) . '" />';

    and this works well in getting the url but i wanted to know how i could specify an exact thumbnail size.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘the_post_thumbnail image sizes’ is closed to new replies.