0

I try to get thumbnail URL correctly...

In my functions.php, I have theses lines:

<?php
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 'emphasis', 132, 82, true );
?>
<?php
function w_thumbnail_src() {
    if (has_post_thumbnail()) {
        $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), 'emphasis');
       echo $thumb[0]; // thumbnail url
    }
}
?>

The thing is, I don't get the correct size in my template :( I have this:

<?php w_thumbnail_src(); ?>

That returns the big image, not the thumbnail :(

Where am I wrong?

Regards, Alex

0

You are not using set_post_thumbnail_size properly. Try with:

add_image_size( 'emphasis', 132, 82, true );
  • I see... Big thanks. What if I don't want to refer to the size in the function, but in the template itself? So I can use the same function for different image size? And set in my template, something like: <?php w_thumbnail_src('emphasis'); ?> Big thanks anyway :) – flintsburg Nov 21 '11 at 16:54
  • You may use the following modified code: function w_thumbnail_src($size = 'thumbnail') { if (has_post_thumbnail()) { $thumb = wp_get_attachment_image_src(get_post_thumbnail_id(), $size); echo $thumb[0]; // thumbnail url } } You may pass the desired size, and if you don't it will use the default thumbnail size. – marcochiesi Nov 21 '11 at 18:08
  • Many thanks Marco. It's working perfectly. And now, I understand how to create "dynamic" functions. So thank you :) – flintsburg Nov 21 '11 at 18:44
2

You are using the wrong function to set the size of the image. You want to use add_image_size('emphasis', 132, 82, true ); set_post_thumbnail_size() sets the default post thumbnail size and does not accept a name attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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