Support

Account

Home Forums Front-end Issues Group 'if' statement to not show fields with no value

Solved

Group 'if' statement to not show fields with no value

  • I’ve got a website with a number of field groups and lots of fields within them. A number of fields for posts haven’t been filled out.

    I want to do a blanket ‘if’ statement over a whole group to say that if a field has no value, then don’t show it on the front end.

    Currently i’m using this code to display all fields:

    <?php $fields = get_fields();
    
              $fields = get_field_objects();
    
              if( $fields )
              {
                foreach( $fields as $field_name => $field )
                {
                  echo '<dl>';
                    echo '<dt>' . $field['label'] . '</dt>';
                    echo '<dd>' . $field['value'] . '</dd>';
                  echo '</dl>';
                }
              }
    
            ?>

    i need an easy way to display all the fields connected with a post type, but i also need to filter out a specific field group and any empty fields.

    using the get_field_objects() function, is there any way to filter out a specific custom field group from being displayed and also to not display empty fields?

    • Elliot

    • September 4, 2013 at 2:23 pm

    Hi @aaronrobb

    A simple if statement will work for you like so:

    
    <?php $fields = get_fields();
    
      $fields = get_field_objects();
    
      if( $fields )
      {
        foreach( $fields as $field_name => $field )
        {
        	if( $field['value'] )
        	{
    		      echo '<dl>';
    		        echo '<dt>' . $field['label'] . '</dt>';
    		        echo '<dd>' . $field['value'] . '</dd>';
    		      echo '</dl>';
          	}
        }
      }
    
    ?>
    
  • Perfect that worked excellently!

    One other question.

    Is there a way to filter out specific field groups with this code? I know there are ways to use the postID to show the groups, but what if we want to show all associated with a post, except one group?? Does that exist?

    • Elliot

    • September 5, 2013 at 12:53 pm

    Hi @aaronrobb

    Sorry, the get_fields function does not have access to the different field groups.

    You will need to re-think your code to find a different way to detect the ‘field_group’.

    Sorry I can’t be of much help on this one, it’s a bit out of scope for the plugin

    Cheers
    E

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.