Yordan Kostadinov
Yordan Kostadinov

Reputation: 139

Hide div container if ACF Repeater sub field is empty

I have a ACF repeater. How can I hide the div with class "brand" if the sub field in the repeater is empty?

<?php if( have_rows('partners', 'option') ): ?>
<div class="brand">
    <div class="container">
        <?php
                while ( have_rows('partners', 'option') ) : the_row(); ?>

                <div class="single-brand">
                    <?php 
                    $partner_logo = get_sub_field('partner_logo', 'option');
                    if( !empty($partner_logo) ): ?>
                        <img src="<?php echo $partner_logo['url']; ?>" alt="<?php echo $partner_logo['alt']; ?>" />
                    <?php endif; ?>
                </div>
        <?php   
                endwhile;

        ?>
    </div>
</div>
<?php endif; ?>

Upvotes: 0

Views: 1331

Answers (1)

Sandra
Sandra

Reputation: 418

I think this will solve your issue.

<?php if( have_rows('partners', 'option') ): ?>
  <?php 
        $flag = 0;
        while ( have_rows('partners', 'option') ) : the_row(); 
           $partner_logo = get_sub_field('partner_logo', 'option');
           if($partner_logo){
              $flag = 1;
           }
        endwhile;
    endif;
?>
<?php if($flag == 1):?>
<div class="brand">
    <div class="container">
        <?php  while ( have_rows('partners', 'option') ) : the_row(); ?>
          <div class="single-brand">
          <?php 
              $partner_logo = get_sub_field('partner_logo', 'option'); ?>
              <img src="<?php echo $partner_logo['url']; ?>" alt="<?php echo $partner_logo['alt']; ?>" />
           </div>
        <?php   
                endwhile;

        ?>
    </div>
</div>
<?php endif; ?>

Upvotes: 1

Related Questions