Home Forums General Issues Adding an image size choice


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']; ?>" />
  • YAY! Thank you so much that worked!! THANK YOU!

  • 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!!

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Adding an image size choice’ is closed to new replies.