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