Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
elmarbransch opened this issue Apr 17, 2020 · 4 comments

Comments

@elmarbransch
Copy link

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
Copy link
Author

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

8dbfcf3

does anyone watch these?

@kleins
Copy link

kleins commented Sep 27, 2021

Please merge this pull request. Thanks

@jackjonesNOW
Copy link

jackjonesNOW commented Mar 23, 2022

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]; ?> 

@chrispink
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants