Lately I wanted to upgrade my blog with featured images/post thumbnails. I'm using a custom theme build on the roots theme (this is not a roots theme issue, I believe). The image I upload is 1024x768 and shall be cropped to letterbox format 1024x512.

This is the code I'm using in the theme-file:

<?php
    if ( has_post_thumbnail() ) {
    the_post_thumbnail(array(1024, 512), array('class' => 'img-responsive')); // add post thumbnail
        }
    ?>

Now, the problem is, that on my local XAMPP box it works perfectly well. But on the webserver images aren't cropped and, they do not span the entire width of the primary content area, as they do on the local install. Checking the code with Firebug, the image is 682px wide and 512px tall (1024 x 512px on the XAMPP box). So what happens is that the image is scaled, not cropped.

I re-uploaded the theme and I disabled all plugins. And of course I searched related topics, tried other code samples, checked that GD is enabled - it is and regenerated thumbs. None of this solved the problem.

Any ideas greatly appreciated.

Per the Codex entry for the_post_thumbnail(), passing an array has not worked since WordPress 3.0:

PLEASE NOTE: The crop does not work in WP 3.0+. All that is needed for WP 3.0+ is the call for the thumbnail to post. Then proceed to media in the dashboard and set your thumbnail to crop to the size you wish to use.

The correct implementation is to create a custom image size with your array values, via add_image_size() inside the Theme setup function (or any other callback hooked into after_setup_theme):

add_image_size( 'custom-size', 1024, 512, true );

...then call that size directly:

the_post_thumbnail( 'custom-size' );

Normally, if you want to crop a thumbnail you have to say so using "true" as third parameter.

array(1024, 512, true)

At least thats how you do it if you defined a thumbnail image size in your functions.php. So I guess it should work in the the_post_thumbnail array too :) Well, and if it doesnt't you could still define a thumbnail size in your functions.php and use that one...

Here's a reference: http://codex.wordpress.org/Function_Reference/set_post_thumbnail_size

Here's what solved the problem (perhaps not the most elegant solution, but working for the time being:

in custom.php (the roots theme's functions.php) I added this:

 if ( function_exists( 'add_theme_support' ) ) {
add_theme_support( 'post-thumbnails' );
    set_post_thumbnail_size(1024, 512, true);

}

in the theme-file where the featured image shall appear I added this:

    if ( has_post_thumbnail() ) {
the_post_thumbnail(array(1024, 512, true), array('class' => 'img-responsive')); // add post thumbnail
    }
?>

What I don't understand is that neither works without the other. Anyway, it solves the issue for now

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.