Home › Forums › General Issues › Adding an image size choice
I apologize upfront if this is a similar question, I tried to those and wasn’t getting anywhere :/ I feel like this should be doable 🙂
I’m trying to give the client a way to choose between a few image choices. What I have so far: I have a flexible layout with an image (set to array), then I have a sub field set up as a ‘select’ that has the dropdown for the image size choices I feel they will need ( I might end up adding more but there are at least 3 now)
Here is the template code I have so far which is obviously wrong; as I know the the_sub_field( ‘image_size’ ) part is wrong… where am I getting off base?
<?php if ( have_rows( 'image' ) ): ?>
<?php while ( have_rows( 'image' ) ) : the_row(); ?>
<?php if ( get_row_layout() == 'image_or_graphic' ) : ?>
<?php $image_choice = get_sub_field( 'image_choice' ); ?>
<?php if ( $image_choice ) { ?>
<img src="<?php echo $image_choice['url']; ?>" alt="<?php echo $image_choice['alt']; ?>" />
<?php } ?>
<?php the_sub_field( 'image_size' ); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else: ?>
<?php // no layouts found ?>
<?php endif; ?>
If the image size value that can be selected matches images sizes on the site and returned in the image array then it would be something like this
<img src="<?php
echo $image_choice['sizes'][get_sub_field('image_size')]; ?>" alt="<?php
echo $image_choice['alt']; ?>" />
Is there a way take this a step further and still have the responsive images srcset to the image size choosen? I think you have to incorporate this code somehow to what I have :
I saw this other post and wondered if I add the "wp-image-{$image_id}"
in if that would solve it.
Thank you!!
The topic ‘Adding an image size choice’ is closed to new replies.
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.