Support

Account

Home Forums General Issues empty rows returning data

Solved

empty rows returning data

  • I have a group field using if (have_rows). There is no data in the field, however, the field is still returning data. Here’s my code:

    	<!-- video section-->
    	<?php if( have_rows('video_feature') ): echo ('has video');?>
    		<section class="video-feature" id="biovid">
    
    	<?php while( have_rows('video_feature') ): the_row(); ?>
    
    		<div class="boxrow">
    			<div class="boxtext">
    				<?php the_sub_field('video_description');?>		
    			</div><!--end boxtext-->
    
    			<div class="video">
    				<?php the_sub_field('video_link');?>
    			</div><!--end video-->
    		</div><!--end boxrow-->
    
    	<?php endwhile; ?>
    		</section>
    	<?php endif; ?>

    Empty content is returning:

    <section class="video-feature id="biovid">
    <div class="boxrow">
    <div class="boxtext">
    </div>
    <div class="video">
    </div>
    </section>
  • The group field is a special type of repeater field, it always has 1 row even if there is no content in any of the sub fields. This means that have_rows() is always true. You must check all of the sub fields. While the have_rows() method will work I would suggest using the basic get_field() approach https://www.advancedcustomfields.com/resources/group/

  • I had tried it with the get_field() option and queried if($field) as well and it still returns a value. So it sounds like I should change it to a repeater field then.

  • OK changing it to a repeater field worked. Thanks!

  • To go back to your previous reply, sorry for the late response, you not only need to check the returned value but you also need to check all the sub fields because the value returned will always be a array and if ($field) will always be true.

    
    $field = get_field('video_feature');
    if ($field && $field['video_description'] && $field['video_description']) //......
    
  • This was exactly was I was looking for, great answer, but for any PHP newbies, who will likely use it with opening and closing tags I added a “revised” Version, I myself hat trouble with the code because I forgot the “:”.

    Anyways perfect Code for the Problem, using Repeater is not an alternative for blocks.

    <?php $field = get_field('video_feature');
    if ($field && $field['video_description'] && $field['video_description']): ?>
  • OH Thanks. yeah I really don’t want it to be a repeater field… so I will try going back to a group and using the newer suggested code.
    Thanks!

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

The topic ‘empty rows returning data’ is closed to new replies.