1

I have a custom field for an image, and using it this way:

<?php $item_img = get_sub_field('image'); ?>
<img src="<?php echo $item_img ?>" />

But I want to use the thumbnail version of the image, as set to 150x150 in wordpress.

How do I do that with this type of setup?

Do I have to set a custom add_image_size() or does wordpress have duplicated thumbmail size images?

  • You can't do that this way, unless you use native WordPress functions to do so. – Jack Johansson Aug 28 '17 at 3:51
0

Since you are using get_sub_field() i assume you are inside a have_rows() (a repeater), in theory it should work like this:

$image = get_sub_field('image');
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />

and this is what you can get:

$image['url'] //this is the full image
echo $image['sizes']['medium']; //medium image
echo $image['sizes']['thumbnail']; //thumbnail image
echo $image['sizes']['yourcustomsize']; //custom sized image

another way is to use:

$size = 'full'; // (thumbnail, medium, large, full or custom size)
echo wp_get_attachment_image( $imageID, $size );

now if these dont work for you, that means your images are still saved as just the URL, not the object. try re-saving the post/page where the image is uploaded or if you have a set of images, you might need to use another plugin to reupload and create the sizes.

0

You can always edit the field in Custom Fields > Field Groups to return the ID. Then you can use it like this:

<img src="<?php echo wp_get_attachment_image_src( get_sub_field( 'image' ), 'thumbnail' )[0]; ?>">

If it's not in your control, and the field is set to only return the url, you can set the 2nd argument of get_sub_field() to false to get the ID, since ACF always saves the ID regardless of that setting, and setting that to false means that you get the raw value.

So that would be:

<?php $image_id = get_sub_field( 'image', false ); ?>
<img src="<?php echo wp_get_attachment_image_src( $image_id, 'thumbnail' )[0]; ?>">

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.