network-media-library icon indicating copy to clipboard operation
network-media-library copied to clipboard

Images in ACF repeaters returns the same image (last one) for any repeating element

Open elmarbransch opened this issue 4 years ago • 4 comments

If you create an ACF repeater containing an image it will correctly return the structure of the sub fields, but the value for image is all the same for each repeater row. And it is the value of the last image in the repeater set.

The fix is fairly easy, but I cannot provide a real PR right now. Instead I can document the required change here. The reason is that the two hooks in place here

add_filter( "acf/load_value/type={$type}", [ $this, 'filter_acf_attachment_load_value' ], 0, 3 );
add_filter( "acf/format_value/type={$type}", [ $this, 'filter_acf_attachment_format_value' ], 9999, 3 );

are not called for each repeating row alternately, but it will call n times load_value and n times format_value. To solve this I simply pushed the values an an array and grab them later from that array.

so I declared:

protected $values = []; // null; and then assigned this way $this->values[] = $image; // $this->value = $image;

and finally pulled the value here return array_shift( $this->values ); // return $this->value; i hope this is easy enough to understand to put this back into the main code repo,

Best -Elmar

elmarbransch avatar Apr 17 '20 17:04 elmarbransch

I found a similar approach (better than mine) in this pull request here

https://github.com/humanmade/network-media-library/pull/67/commits/8dbfcf36bdca8a9e657fb9a57a82494aa491ec4e

does anyone watch these?

elmarbransch avatar Apr 17 '20 19:04 elmarbransch

Please merge this pull request. Thanks

kleins avatar Sep 27 '21 18:09 kleins

I found it quite difficult to get the image url or post object to display using flexible content. My work around was to comment out this ACF addition and just call in the image via the ID. and do something similar to this:


<?php $hero = get_sub_field('background_image');
$size = 'large';
$image = wp_get_attachment_image_src($hero, $size); ?>
<?php echo $image[0]; ?> 

jackjonesNOW avatar Mar 23 '22 11:03 jackjonesNOW

Just to note this is also an issue with Flexible Content fields where the image returned in all positions is the last one in the Flexible Content set. Presumably the same issue.

chrispink avatar Sep 28 '23 12:09 chrispink