WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] the_post_thumbnail image sizes (4 posts)

  1. miocene22
    Member
    Posted 3 years ago #

    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 260x160.

    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?

  2. miocene22
    Member
    Posted 3 years ago #

    any ideas anyone?

  3. miocene22
    Member
    Posted 3 years ago #

    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

  4. rw1
    Member
    Posted 3 years ago #

    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)

    thanks!

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic