4

I want a thumbnail of the featured image of the posts as a URL. So I'm using the following code to get a smaller size of the featured image.

$post[$i]['thumbnail'] = wp_get_attachment_url(get_post_thumbnail_id($post[$i]['id'], 'thumbnail'));

However all the time it returns the full size image, I even tried,

 $post[$i]['thumbnail'] = wp_get_attachment_url(get_post_thumbnail_id($post[$i]['id'], array(120,140)));

But still it returns the large image. How can I fix this?

1

3 Answers 3

4

Hey First add this line into your function.php file

add_image_size( 'custom-size', 120, 140 );

Then call custom-size like this

<?php $thumb_id = get_post_thumbnail_id( $id );
                if ( '' != $thumb_id ) {
                    $thumb_url  = wp_get_attachment_image_src( $thumb_id, 'custom-size', true );
                    $image      = $thumb_url[0];
                }?>
            <img src="<?php echo $image;?>">

I think it work fine

4
  • but what is 'thumbnail' is also returning the full size of the image? not a thumbnail size?
    – rksh
    Dec 19, 2014 at 8:44
  • hey thumbnail size is define in wordpress admin panel settings->media->Thumbnail-size please check it what size define in media for thumbnail Dec 19, 2014 at 8:46
  • @amrith it's set to the default 150x150 but i'm not retrieving that i'm getting the full size of it
    – rksh
    Dec 19, 2014 at 9:14
  • hey rksh please check the edited answer its work for me Dec 19, 2014 at 9:30
4

You can try using the wp_get_attachment_image_src()

$thumb_id = get_post_thumbnail_id($post[$i]['id']);
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail', true);
$post[$i]['thumbnail'] = $thumb_url_array[0];

In the above code, wp_get_attachment_image_src() returns an array with image src, height and width, $thumb_url_array[0] is your thumbnail.

1
  • much prefer this solution as it can be used outside of a theme file Jan 28, 2016 at 17:27
1

The Syntax is defined as

<?php echo get_the_post_thumbnail( $post_id, $size, $attr ); ?> 

You can get default wordpress thumbnail size (150X150) by following code

<?php get_the_post_thumbnail( $post_id, 'thumbnail' ); ?>

If you want a custom size thumbnail, then use bellow code :

<?php get_the_post_thumbnail( $post_id, array( 200, 200) ); ?>

For further reference visit wordpress codex Click Here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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