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


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">
                        $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>
                            $active = '';
                            $num += 1;
                        endwhile; ?>
                    <!-- Wrapper for slides -->
                    <div class="carousel-inner" role="listbox">
                        $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><!-- /item -->
                            <?php $active = '';
        </div><!-- /row -->
    <?php endif; ?>

    Many thanks in advance!

  • before outputting the line item in each loop

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

You must be logged in to reply to this topic.