ACF Pro, Latest Version.
Trying to hide all the output if there are no images added to the sub field of this repeater field:
<?php if( have_rows('award_images', 'options', true) ): ?>
<section id="awards">
<div class="container">
<div class="row">
<h2 class="awards text-center"><span>Awards</span></h2>
<div class="awards-wrap col-md-12">
<ul class="awards">
<?php
while( have_rows('award_images', 'options', true) ): the_row();
// vars
$award_image = get_sub_field('award_image');
$award_image_link = get_sub_field('award_image_link');
?>
<li class="award">
<?php if( $award_image_link ): ?>
<a href="<?php echo $award_image_link; ?>">
<?php endif; ?>
<img src="<?php echo $award_image['url']; ?>" alt="<?php echo $award_image['alt'] ?>" />
<?php if( $award_image_link ): ?>
</a>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
</div>
</div>
</div>
</section>
<?php endif; ?>
Specifically, the award_image field, as the links are already hidden if they’re empty, my thought is that it’s because the var isn’t declared above the beginning of the output.
I solved this for anyone else that has the same problem. It was basically my ignorance and oversight:
<section id="awards">
<?php if( have_rows('award_images', 'options', true) ): ?>
<div class="container">
<div class="row">
<h2 class="awards text-center"><span>Awards</span></h2>
<div class="awards-wrap col-md-12">
<ul class="awards">
<?php
while( have_rows('award_images', 'options', true) ): the_row();
// vars
$award_image = get_sub_field('award_image');
$award_image_link = get_sub_field('award_image_link');
?>
<li class="award">
<?php if( $award_image_link ): ?>
<a href="<?php echo $award_image_link; ?>">
<?php endif; ?>
<img src="<?php echo $award_image['url']; ?>" alt="<?php echo $award_image['alt'] ?>" />
<?php if( $award_image_link ): ?>
</a>
<?php endif; ?>
</li>
<?php endwhile; ?>
</ul>
</div>
</div>
</div>
<?php endif; ?>
</section>
I just moved the if (have_rows() ) line inside the section element. 🙂
The topic ‘Repeater: Hide output if sub_field is empty’ 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.