Support

Account

Home Forums ACF PRO Repeater: Hide output if sub_field is empty

Solved

Repeater: Hide output if sub_field is empty

  • 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. 🙂

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Repeater: Hide output if sub_field is empty’ is closed to new replies.