Support

Account

Home Forums ACF PRO New Attachment Field – Update images

Solving

New Attachment Field – Update images

  • I have a page that uses an ACF gallery.
    I’ve also added a field to all image attachments called “gallery_size”.
    This offers the options of Small, Medium or Large via a radio button.
    My end goal is to use the size value to vary the width of the image in the gallery.

    I have the gallery working, I can loop through the images and grab their descriptions and the size value. That’s all good.

    The problem I have is that there are 300 images on the site and the “gallery_size” value is only becomes available if I open the image in the media gallery and update it.

    Is there any way to auto update all the images rather than me having to do this manually?

    Thanks in advance for any pointers!

  • Hi there,

    Thanks for getting in touch!

    You can workaround this issue by creating a custom foreach loop to step through all the gallery images and pass the field value to the update_field() function. The first parameter should be the field key as described on the following page: https://www.advancedcustomfields.com/resources/update_field/

    I hope this info helps. Thank you.

  • Hi James,

    Thanks or your reply. However having had a few attempts I’m ot sure I fully understand what I need to do.

    I have a gallery on some certain page tmeplates and in a custom post type.

    <?php //Gallery images
    
    			$images = get_field('image_gallery');
    			$size = 'full';
    
    			if( $images ): ?>
    			      <div id="masonry-loop">
    			      	<div class="grid-sizer"></div>
        					<div class="gutter-sizer"></div>
    					<?php foreach( $images as $image ): ?>
    						<img class="grid-entry" src="<?php echo $image['url'];?>" alt="<?php echo $image['alt']; ?>" />
    					<?php endforeach; ?>
    			      </div>
    			<?php endif; ?>

    Do I work the update_field into the gallery loop?
    For example:

    <?php //Gallery images
    
    			$images = get_field('image_gallery');
    			$size = 'full';
    			$count = (int) get_field('field_59b6acbe2a8g6');
    
    			if( $images ): ?>
    			      <div id="masonry-loop">
    			      	<div class="grid-sizer"></div>
        					<div class="gutter-sizer"></div>
    					<?php foreach( $images as $image ): ?>
    			// increase
    			$count++;
    			// update
    			update_field('field_59b6acbe2a8g6', $count);						
    			<img class="grid-entry" src="<?php echo $image['url'];?>" alt="<?php echo $image['alt']; ?>" />
    					<?php endforeach; ?>
    			      </div>
    			<?php endif; ?>

    If so I can’t seem to get it to work.
    Any pointers would be greatly appreciated.

    Many thanks,

  • I just replied but it seems to have vanished.

    Thanks for your reply, however after a few attempts I’m stuck and not sure where to place the foreach loop you mention.

    I have a galley (I’ve stripped back the code here)

    <?php //Gallery images
    
    $images = get_field('image_gallery');
    $size = 'full';
    
    if( $images ): ?>
    	<?php foreach( $images as $image ): ?>
    		<img class="grid-entry" src="<?php echo $image['url'];?>" alt="<?php echo $image['alt']; ?>" />
    	<?php endforeach; ?>
    <?php endif; ?>

    Could I weave the the update_field into that foreach loop?
    Or does it need to be somewhere else?

    e.g.

    <?php //Gallery images
    
    $images = get_field('image_gallery');
    $size = 'full'; //
    
    $count = (int) get_field('field_59b6acbe2a8d2');
    
    if( $images ): ?>
    	<?php foreach( $images as $image ): 
    		// increase
    		$count++;
    		// update
    		update_field('field_59b6acbe2a8d2', $count); ?>
    		<img class="grid-entry" src="<?php echo $image['url'];?>" alt="<?php echo $image['alt']; ?>" />
    	<?php endforeach; ?>
    <?php endif; ?>

    Once that is working, does it only take a refresh of the page for it to update the images?

    Many thanks

  • Hi there,

    The above code should be able to update the value but the post will need to be published for the change to take effect.

  • Hi again, I’m just coming back to this once more!

    I realised I’m not 100% which field I should be updating.

    I have a Radio Button (size) that is an option on image attachments.
    I also have a Gallery field that holds those images.

    I’d like the images to take the default value (med) when they are uploaded or a page with a gallery is published (as the gallery might contain images that don’t have a value set by the user yet)

    Do I use the update_field() function on the gallery field or the radio button?

    I hope that makes sense…

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

The topic ‘New Attachment Field – Update images’ is closed to new replies.