Support

Account

Home Forums ACF PRO New Attachment Field – Update images Reply To: New Attachment Field – Update images

  • 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