Tomek Buszewski
Tomek Buszewski

Reputation: 7935

Can't get WordPress' custom image sizes

I defined my custom image sizes like this:

  add_theme_support('post-thumbnails');
    add_image_size('news-big', 370, 240, true);
    add_image_size('news-small',270,150,true);
    add_image_size('portfolio-big',370,500,true);
    add_image_size('portfolio-small',270,350,true);
    add_image_size('client',200,150,false);

I uploaded test photos and it works - they are resized. But when I try to print it:

    <?php $img = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID, 'news-big') ); ?>
    <?php print_r($img); ?>

It returns 'thumb' (150x150). It's first time I see something like this. I use Roots framework theme. What's wrong?

Upvotes: 6

Views: 9753

Answers (3)

jondavidjohn
jondavidjohn

Reputation: 62392

get_post_thumbnail_id takes only one argument, the post id...

You want to pass your size argument to the wp_get_attachment_image_src

$img_src = wp_get_attachment_image_src(
             get_post_thumbnail_id($post->ID), 
             news-big'
           );

Why?

Even though WordPress makes many images per your size definitions, there is still only a single attachment id. There are not different ids for the different sizes.

Upvotes: 2

Alexander
Alexander

Reputation: 111

This is wrong displaying of thumdnail:

<?php $img = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID, 'news-big') ); ?>
<?php print_r($img); ?>

If u add thumnail to post u may simple use:

the_post_thumbnail();

without this all other things. And if u add custome thumbnail size u may use:

the_post_thumbnail('news-big');

But before use it u must really add image to post as "Featured image".

Upvotes: 0

Jared
Jared

Reputation: 12524

You have a misplaced bracket, so you weren't actually passing a size to wp_get_attachment_image_src and it was defaulting to thumbnail.

<?php $img = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'news-big' ); ?>

Upvotes: 9

Related Questions