Support

Account

Home Forums Front-end Issues Hide Repeater Filed Row if empty

Helping

Hide Repeater Filed Row if empty

  • Hi

    I have a CPT with a Repeater Filed called files.

    This Repeater Field contains only a sub-filed (a Text filed) called file.

    There Repeater Fields I set are in total 10 and are populated from somewhere outside the website and sometimes some rows are not populated and stay empty. This cannot be changed.

    For example in one post only 3 rows have value but the rest are empty.

    In front end I am getting the values in a slider. It works, but also the empty fields are rendered and if the row is empty it shows a missing file (images in my case).

    How can I hide empty fields taking in consideration the code below?

    <?php if( have_rows('files') ): ?>
        <div class="slider">
            <div class="container">
                <div id="slider-basic" class="slider slide" data-ride="carousel">
                    <!-- Indicators -->
                    <ol class="carousel-indicators">
                        <?php
                        $active = 'active';
                        $num = 0;
                        while ( have_rows('files') ) : the_row();
                            ?>
                            <li data-target="#slider-basic" data-slide-to="<?php echo $num ?>" class="<?php echo $active ?>"></li>
                            <?php
                            $active = '';
                            $num += 1;
                        endwhile; ?>
                    </ol>
                    <!-- Wrapper for slides -->
                    <div class="carousel-inner" role="listbox">
                        <?php
                        $active = 'active';
                        while ( have_rows('files') ) : the_row();
                            ?>
                            <div class="carousel-item <?php echo $active ?> special">
                                <div class="container">
                                    <img src="<?php echo get_template_directory_uri(); ?>/files/images/<?php the_sub_field('file'); ?>" />
                                </div>
                            </div><!-- /item -->
                            <?php $active = '';
                        endwhile;
                        ?>
                    </div>
                </div>
            </div>
        </div><!-- /row -->
    <?php endif; ?>

    Many thanks in advance!

  • before outputting the line item in each loop

    
    if (empty(get_sub_field('file'))) {
      continue;
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.