Support

Account

Home Forums ACF PRO Show fields in UL, but handle empty fields too

Helping

Show fields in UL, but handle empty fields too

  • So I have this field group called ‘Contact details’. It contains the following fields:

    • Contact person (Name – contact_person; Type – Text)
    • Telephone number (landline) (Name – telephone_number_landline; Type – Text)
    • Telephone number (mobile) (Name – telephone_number_mobile; Type – Text)
    • Email address (Name – email_address; Type – Email)

    I would like to have it displayed in a list (UL or OL), but only those fields I populate with data.

    I understand what’s behind this code for not showing a field if there’s no data in it (found at https://www.advancedcustomfields.com/resources/hiding-empty-fields/):

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

    But the loop on that page (which looks like what I’m after)…

    <?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; ?>

    What the heck am I supposed to do with that?

    Can I modify that loop to fit my need?

    Please help. My PHP skillz are beyond shocking.

  • Any help here? Really need it.

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

You must be logged in to reply to this topic.