Support

Account

Home Forums Add-ons Repeater Field Display a field only if populated

Solved

Display a field only if populated

  • I would like to display a field only if populated. My current code is below. The only probably is not every item will have a title. As it is now when a title isn’t entered blank h2 tags are still visible in the front end.

    I have seen similar posts directing me to this post: http://www.advancedcustomfields.com/resources/tutorials/working-with-nested-repeaters/

    However that still does not help me, any help is greatly appreciated.

    <?php while(the_repeater_field('content_block')): ?>
    <div class="clearfix">
    <h2><?php the_sub_field('title'); ?></h2>
    <div class="img"><img src="<?php the_sub_field('image'); ?>"></div>
    <div class="txt"><?php the_sub_field('content'); ?> </div>
    </div>
    <?php endwhile ?>
  • Hi @Permanence

    Have you read the code exmaples for conditional logic / understand what an if statement in PHP is?

    Your code should look more like this:

    
    <?php while(the_repeater_field('content_block')): ?>
    <div class="clearfix">
    <?php if( get_sub_field('title') ): ?>
    	<h2><?php the_sub_field('title'); ?></h2>
    <?php endif; ?>
    <div class="img"><img src="<?php the_sub_field('image'); ?>"></div>
    <div class="txt"><?php the_sub_field('content'); ?> </div>
    </div>
    <?php endwhile ?>
    
  • Thanks Elliot, yes I do know what an if statement is but what I was trying was not doing the job. My knowledge of php isnt great!

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

The topic ‘Display a field only if populated’ is closed to new replies.