Apalabrados
Apalabrados

Reputation: 1146

get_post_thumbnail with certain dimensions

I'm trying to get the attached images from posts with 250px width (the image size for thumbnails).

In the function.php file within the theme, I have the following lines:

add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 250, 200 );

so, in the uploads folder, I can see that besides the main image file, other format are generated, like filename_250x200.jpg and so on...

The question is that when I try to get the post thumbnail, i receive the original image.

This is the code:

wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'post-thumbnails' ));

What's wrong?

Upvotes: 2

Views: 1675

Answers (2)

Spartacus
Spartacus

Reputation: 1508

wp_get_attachment_url doesn't accept a size parameter. You need get_the_post_thumbnail

echo get_the_post_thumbnail($post->ID,'post-thumbnails');

Upvotes: 1

Mr. Hugo
Mr. Hugo

Reputation: 12590

You are confusing set_post_thumbnail_size with add_image_size. You should use the latter. Add this to your functions.php:

add_image_size( 'custom-size', 250, 200 ); // 250 pixels wide by 200 pixels tall, soft proportional crop mode

And then call in your template:

the_post_thumbnail( 'custom-size' ); 

More info: https://developer.wordpress.org/reference/functions/add_image_size/

Upvotes: 2

Related Questions