Home › Forums › Add-ons › Repeater Field › Conditional content for remaining repeater rows
Apologies if this has been asked already, I couldn’t find anything on the subject unfortunately.
Let’s say I have a repeater field that permits 4 maximum rows/instances on a given page. I’d like to have some kind of conditional fallback content for the rows that haven’t been used.
For example, in a scenario where the user has added content to only 1 of the 4 rows, I’d like the output to be:
<div>Content added via ACF</div>
<div>Nothing to see here</div>
<div>Nothing to see here</div>
<div>Nothing to see here</div>
For content added to only 2 of the 4 rows, I’d like:
<div>Content added via ACF</div>
<div>Content added via ACF</div>
<div>Nothing to see here</div>
<div>Nothing to see here</div>
and so on.
Hopefully that makes sense?
I did toy around with the idea of creating 4 x standard field groups, all with the exact same set of fields within them, but thought there must be a more elegant way of handing this using some kind of conditional/if statement?
Any ideas?
Hi @paulb
This will be pretty easy to code.
Can you provide the snippet you are using to loop through and render the data currently?
All you need to do is add a counter so that after the loop, you know how many rows were added. Then make a new ‘for loop’ to do the remaining rows and add your custom ‘Nothing’ text.
Thanks
E
Thanks for the swift reply, Elliot.
The basis of my repeater code is essentially:
<?php if(get_field('featured_listing')) : ?>
<?php while(has_sub_field('featured_listing')): ?>
<div class="promo">
<h3><a href="<?php the_sub_field('featured_url'); ?>" target="_blank"><?php the_sub_field('featured_name'); ?></a></h3>
<div class="button"><a href="<?php the_sub_field('featured_url'); ?>">Visit website</a></div>
</div>
<?php endwhile; else: ?>
<div class="promo empty">
<p>Nothing to see here, why not <a href="#">add a promo</a>?</p>
</div>
<?php endif; ?>
I appreciate this is probably all completely wrong! 🙂
Thanks again,
Paul
Hi @paulb
Here is what your code hsould look like:
<?php
$i = 0;
$min = 4;
if(get_field('featured_listing')) : ?>
<?php while(has_sub_field('featured_listing')): $i++; ?>
<div class="promo">
<h3><a href="<?php the_sub_field('featured_url'); ?>" target="_blank"><?php the_sub_field('featured_name'); ?></a></h3>
<div class="button"><a href="<?php the_sub_field('featured_url'); ?>">Visit website</a></div>
</div>
<?php endwhile; ?>
<?php endif: ?>
<?php if( $i < $min ): ?>
<?php for( $j = 0; $j < ($min-$i); $j++ ): ?>
<div class="promo empty">
<p>Nothing to see here, why not <a href="#">add a promo</a>?</p>
</div>
<?php endfor; ?>
<?php endif; ?>
Please note I have not tested this code, but you can see how the logic should work.
Thanks
E
Elliot – you are a gentleman and a scholar.
Only had to swap the first <?php endif: ?> for <?php endif; ?>, and then it worked perfectly. This is exactly what I needed!
Thanks so much.
Paul
The topic ‘Conditional content for remaining repeater rows’ is closed to new replies.
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.