Home › Forums › Front-end Issues › Outputting custom image size
I know there’s a similar thread on this, but I’m none-the-wiser and all the solutions for other cases don’t work for me. It’s quite a simple thing I’m trying to do.
I have a custom thumb size set in WP functions called ‘slide-thumb’ and have set it up as a separate field to specify as it seems the only way, but ACF still won’t output it.
My ACF loop is:
<div class="flexslider"> <?php $thumb = wp_get_attachment_image_src(get_sub_field('thumb'), 'slide-thumb' ); ?> <?php if(get_field('activity_slider')): ?> <ul class="slides"> <?php while(has_sub_field('activity_slider')): ?> <li data-thumb="<?php $image = get_field('photo'); echo($image['sizes']['slide-thumb']); ?>"> <img src="<?php the_sub_field('photo'); ?>" alt="<?php the_sub_field('photo_title'); ?>" /> </li> <?php endwhile; ?> </ul> <?php endif; ?> </div>
Image field is set to Image ID and the thumb size shows as selected:
Any help appreciated on getting the square thumbnail to be output.
I had some issues with that to i actually set 3 different sizes
“custom-thumb”, “feature-img” and “main-img” in
function.php but unlike what you said I use a single field which generate all 3 sizes.
I set the return value to
and I am able to get the images in the front end like that:
$attachment_id = get_field('you_field_name'); //in the $custom_thumb var i get the custom_thumb infos replace "product-thumb with the name of your custom size you set in function.php $custom_thumb = wp_get_attachment_image_src( $attachment_id,"product-thumb" ); //then you are able to output the following values from the $custom_thumb var: // the url: $custom_thumb; // the width: $custom_thumb; // the height: $custom_thumb; //use these variable as you like on your html <img src="<?php echo $custom_feature; ?>" /> //or <img src="<?php echo $custom_thumb; ?>" width="<?php echo $custom_thumb; ?>" height="<?php echo $custom_thumb; ?>" /> //you can do the same with any other custom sizes or with the original sizes //just change the last argument "product-thumb" to what you need on that line (the $attachement_id should be declared as seen above). $your_var = wp_get_attachment_image_src( $attachment_id,"your-custom-size" ); //then $your_var shoud have the data you want.
you can still have different fields to upload your images and declare 3 times
$attachment_id_1 = get_field(‘you_field_thumb’);
$attachment_id_2 = get_field(‘you_field_feature’);
$attachment_id_3 = get_field(‘you_field_custom’);
but that’s only necessary if the images are different if it’s the same image the 3 sizes gets generated when you upload the first time.
then to control the crop you can add that little plugin
it does a good job.
Thanks for the help! Your time is much appreciated.
I’m still getting nothing returned in the src=”” field of the image.
I have the image field in question set up as a set up as a repeater field. It’s a child of the main group called ‘activity_slider’ with the image field called ‘photo’. So,
My code reads like this:
$attachment_id = get_sub_field('photo'); // the name of the sub field. Also tried without 'sub'. $custom_thumb = wp_get_attachment_image_src( $attachment_id,"slide-thumb" ); // this is the name of the thumbnail size in functions.php <img src="<?php echo $custom_feature; ?>" />
Nothing is returned. Tried using $custom_thumb too, but no avail.
This is what you said to do right?
Thanks again. Martin.
is the following line missing in your code?
while ( have_rows('your_repeater_row') ) : the_row();
so you may want to try like that to get the sub field:
<?php while ( have_rows('your_repeater_row') ) : the_row(); $attachment_id = get_sub_field('photo'); $custom_feature = wp_get_attachment_image_src( $attachment_id, "slide-thumb" ); ?> <img src="<?php echo $custom_feature; ?>" /> <?php endwhile; ?>
Thanks for this snippet – it did the job! Appreciated. Martin
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.