Hiding empty fields

Last updated Sep 10, 2013

Overview

It is a common task to hide empty field values from your template. This can be achieved by using conditional statements to check the value before rending it.

The basics to conditional a statement is to use an if statement. You can read more about this core PHP function here: http://php.net/manual/en/control-structures.if.php

Examples

Example 1: Basic

This example shows how to check for a value before displaying it. Please note the use of both get_field() and the_field().

<?php if( get_field('field_name') ): ?>
    <p>My field value: <?php the_field('field_name'); ?></p>
<?php endif; ?>

Example 2: Loop

This example shows how the above methodology can be used within a loop. Please note the use of get_field_objects() to load the array.

<?php 

$fields = get_field_objects();

<?php if( $fields ): ?>
    <ul>
    <?php foreach( $fields as $field ): ?>

        <?php if( $field['value'] ): ?>
            <li><?php echo $field['label']; ?>: <?php echo $field['value']; ?></li>
        <?php endif; ?>

    <?php endforeach; ?>
    </ul>
<?php endif; ?>
Supercharge Your Website With Premium Features Using ACF PRO

Speed up your workflow and unlock features to better develop websites using ACF Blocks and Options Pages, with the Flexible Content, Repeater, Clone, Gallery Fields & More.

Explore Features View Pricing

PRO Features
ACF Blocks
Options Pages
PRO Fields
Repeater
Flexible Content
Gallery
Clone

Related