Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Chris Cooper
Chris Cooper
23,959 Points

Can you have the_post_thumbnail different sizes in the loop? Using the custom fields plugin?

Hi All,

I'm creating a news site for the company I work for, the homepage being showing all our most recent posts. The site is designed to have the_post_thumbnail showing with the title of the post over the top of it. 4 pictures per row.

What I'm now trying to do is have 2 different sizes, using the_post_thumbnail('');

I have created 2 featured image sizes in functions.php:

add_image_size( 'featured-normal', 334, 274, true ); add_image_size( 'featured-large', 675, 548, true );

I have also created a custom field, called "featured-image-size" that means you can select from the 2 (featured-normal & featured-large).

My hope was that you could select from either featured-normal or featured-large and then add that into the post thumbnail.

Is something like this possible: the_post_thumbnail( the_field('featured-image-size'); );

Any help would be mightily appreciated.

Cheers

1 Answer

Tammy Hart
Tammy Hart
2,819 Points

You can use the custom field data to populate the thumbnail size following this example:

$thumbnail_size = get_post_meta( $post->ID, 'featured-image-size', true );

the_post_thumbnail( $thumbnail_size );

there is a function called the_field() with the Advanced Custom Fields plugin. If that is what you're using to create the size selector, then your initial example was almost correct. the_field() will echo the value of the field you request. What you need is get_field(), which will return it.

Chris Cooper
Chris Cooper
23,959 Points

Hi Tammy, thanks for your help,

still struggling a bit i'm affraid. when i try and do:

<?php 

$thumbnail_size = get_post_meta( $post->ID, 'featured-image-size', true );

the_post_thumbnail( $thumbnail_size );

 ?>

nothing happens, the images lose all sizing, so i tried:

<?php 

$thumbnail_size = get_post_meta( $post->ID, 'featured-image-size', true );

echo $thumbnail_size;

 ?>

it just echos "Array Array Array Array Array", I have 5 posts in the loop so that makes sense to have 5 of them.

However obviously when i try and put that into the the_post_thumbnail via the $thumbnail_size from above it doesn't work.

Any ideas why?

Chris Cooper
Chris Cooper
23,959 Points

iv sorted it now when i did print_r i got:

Array ( [0] => featured-large ) Array ( [0] => featured-normal ) Array ( [0] => featured-normal ) Array ( [0] => featured-normal ) Array ( [0] => featured-normal )

so that lead me to try:

<?php 

$thumbnail_size = get_post_meta( $post->ID, 'featured-image-size', true );

the_post_thumbnail($thumbnail_size[0]);

 ?>

Which gets the images out with the different sizes which is great!

Thank you!

Tammy Hart
Tammy Hart
2,819 Points

Glad you got it figured out!