Support

Account

Home Forums General Issues Help with hiding blank field questions

Solved

Help with hiding blank field questions

  • I have a site with a form created with gravity forms. The visitor fills out this form and it populates a custom post type. The fields that the user completes are custom fields that I have created with “Advanced custom fields”.

    The form has a series of questions that the user fills out, but will leave some blank, as they are not all required.

    This is the code used for the output: <div class="form-question"><?php echo 'Birthday:' ?></div> <div class="form-answer"><?php the_field('birthday'); ?></div>

    I need to figure out how to hide the text ‘Birthday’ if the birthday field is left blank.

  • Perhaps something like so:

    
    <?php if( !empty( get_field('birthday') ) ): ?>
        <div class="form-question">
            <?php echo 'Birthday:'; ?>
        </div> 
        <div class="form-answer">
            <?php the_field('birthday'); ?>
        </div>
    <?php endif; ?>
    

    Or maybe you need to display the .form-question divs:

    
    <div class="form-question">
        <?php echo get_field('birthday') ? 'Birthday:' : ""; ?>
    </div> 
    <div class="form-answer">
        <?php the_field('birthday'); ?>
    </div>
    

    Just in case this looks a bit strange to you, there’s a rundown on ternary logic over here.

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

The topic ‘Help with hiding blank field questions’ is closed to new replies.