Home › Forums › Front-end Issues › Display field groups in separate lists
I have three field groups, named General Specifications, Standard Features, and Optional Extras.
In my code, I want to specify one field group at a time and output its field labels and values using the code below.
I don’t know how to select one field group at a time in my code – that’s where I’m getting stuck :/ I’d appreciate your guidance here, everyone!
Also – I’m a PHP beginner – trying to make things work 🙂
<?php
$fields = get_field_objects();
if( $fields )
{
foreach( $fields as $field_name => $field )
{
echo '<li>';
echo $field['label'] . ": ";
echo $field['value'];
echo '</li>';
}
}
?>
You could do this by looping through the fields 3 times and checking the parent. The value of $field['parent']
will be the group key for the group the field is in. So for example if your group key for the first group is group_5664a3d9a775b
foreach ($fields as $field_name => $field) {
if ($field['parent'] != 'group_5664a3d9a775b') {
// not in first group
continue;
}
// display field
}
// to 2 more times for each of the other groups
Hey John,
Thanks for helping out here! I’ve taken your code and tried to get it working. On my side the code is not running – the resulting html just leaves me with the opening and closing
What am I doing wrong?
<ul>
<?php
$fields = get_field_objects();
if( $fields ) {
foreach ($fields as $field_name => $field) {
if ($field['parent'] != 'group_5937f81f79c5b') {
// not in first group
continue;
}
echo '<li>';
echo $field['label'] . ": ";
echo $field['value'];
echo '</li>';
}
}
?>
</ul>
I was on 5.5.10, but just updated to 5.5.14. I get the same result.
The only think I can think by looking at your code is that none of your fields are in that group. Try displaying the list of fields to see what’s going on.
$fields = get_field_objects();
echo '<pre>'; print_r($fields); echo '</pre>';
John, thanks so much for helping out.
I’ve run the code you suggested, and you can see the results here (under the heading Optional Extras – the other two columns are coded line by line – not ideal): https://bushlapa.storiesandscience.co.za/caravans/goggas/rooimier/
Just to take a step back – what I’d like to achieve is the following:
– Display the field names and values from one field group at a time.
– Exclude any fields that do not have a value.
Here is a snippet of the printed result:
Array
(
[dry_weight] => Array
(
[ID] => 439
[key] => field_5937f37f96261
[label] => Dry weight
[name] => dry_weight
[prefix] => acf
[type] => text
[value] => 620
[menu_order] => 0
[instructions] =>
[required] => 1
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => 303
[wrapper] => Array
(
[width] => 33
[class] =>
[id] =>
)
[_name] => dry_weight
[_prepare] => 0
[_valid] => 1
[default_value] =>
[placeholder] =>
[prepend] =>
[append] => kg
[maxlength] =>
)
[total_length] => Array
(
[ID] => 440
[key] => field_5937f3b496262
[label] => Total length
[name] => total_length
[prefix] => acf
[type] => text
[value] => 3360
[menu_order] => 1
[instructions] =>
[required] => 0
[id] =>
[class] =>
[conditional_logic] => 0
[parent] => 303
[wrapper] => Array
(
[width] => 33
[class] =>
[id] =>
)
[_name] => total_length
[_prepare] => 0
[_valid] => 1
[default_value] =>
[placeholder] =>
[prepend] =>
[append] => mm
[maxlength] =>
)
That’s interesting, when I output something similar the parent for me is the group key. In your case the parent is the post ID of the field group. I don’t know what the difference is, but whatever is in the parent is what you’ll need to look for.
Thanks again – I’ve implemented the code successfully, and also added an additional line of code so that empty fields do not display. Here’s the final code.
<ul>
<?php
$fields = get_field_objects();
if( $fields ) {
foreach ($fields as $field_name => $field) {
if ($field['parent'] == '303') {
if ($field['value'] == '') {
continue;
}
echo '<li><b>';
echo $field['label'] . ": </b>";
echo $field['value'];
echo '</li>';
}
}
}
?>
</ul>
The topic ‘Display field groups in separate lists’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.