Home › Forums › Add-ons › Repeater Field › Get galleries in repeater from Gutenberg Block
Hi,
I’m working on my personal portfolio site, and for the individual project pages I want to be able to create a flexible number of galleries in a repeater, that I can then add one by one to the page with help of Gutenberg blocks.
I have the fields added and the block all set up, but for some reason I can just not manage to get the galleries.
This is what I have so far, based on an old example found on the forum:
<!-- CHECK OUT REPEATER "project_galleries" -->
<?php if( have_rows('project_galleries') ): ?>
<!-- CHECK TO SEE IF IT HAS ROWS IN IT -->
<?php while ( have_rows('project_galleries') ) : the_row(); ?>
<!-- DISPLAY THE TEXT-FIELD THEN ASSIGN OUR GALLERY TO VARIABLE $images -->
<?php
$images = get_sub_field('project_gallery');
if( $images ):?>
<?php foreach( $images as $image ): ?>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
<p><?php echo $image['caption']; ?></p>
<?php endforeach; ?>
<?php endif;
endwhile;
else :
// no rows found
echo 'Nothing found'
endif; ?>
I would really appreciate any help on this!
Check that the “project_galleries” field in ACF is set up as a REPEATER, and inside it, “project_gallery” is configured as a gallery.
To work with Gutenberg blocks, you can try using the “get_field()” function instead of “have_rows()”. Try this:
<?php
$galleries = get_field('project_galleries');
if($galleries):
foreach($galleries as $gallery):
$images = $gallery['project_gallery'];
if($images):
foreach($images as $image): ?>
<a href="<?php echo $image['url']; ?>">
<img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" />
</a>
<p><?php echo $image['caption']; ?></p>
<?php endforeach;
endif;
endforeach;
endif; ?>
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.