Advanced Custom Fields Group Names
You can use this neat little bit of code to display Group names with their respective values — there’s no way to do this within the ACF framework, so I made this!
It’s based on the example here in the ACF documentation. It loops through any number of specified Groups and won’t show any empty Groups or values.
I’m using my own Group and Field names in the examples below; you should substitute your own!
In order for this code to work, you must install Advanced Custom Fields and create at least one Group. It also helps if you are comfortable editing PHP theme files.
- Make a note of the Group names you want to include and their IDs. You can find the Group’s ID in the URL when you’re in “edit” mode. (It’s labeled in the URL as
post=123
.)- Necklace (63)
- Bracelet (290)
- Earrings (77)
- Create a new Group containing each name and ID. (Make a note of this new Group’s ID as well.)
- PP Groups (293)
- Fill in the PP Groups fields, making an entry for each Group you want to display.
- Field type: Text
- Field label: Necklace
- Field name: necklace
- Default value: 63
- This is where you’ll need the new Group’s ID. Replace XYZ with this ID (line 23), then paste this code into your template where you want your ACF fields to be displayed.
I put mine in/themes/MYTHEME/woocommerce/single-product/meta.php
, between the opening and closingdo_action()
hooks.
<?php
function get_specifications_fields($specifications_group_id) {
$specifications_fields = array();
$fields = acf_get_fields($specifications_group_id);
foreach ($fields as $field) {
$field_value = get_field($field['name']);
if ($field_value && !empty($field_value)) {
$specifications_fields[$field['name']] = $field;
$specifications_fields[$field['name']]['value'] = $field_value;
}
}
return $specifications_fields;
}
?>
<div class="row">
<h3 onclick="toggleMore('pp-toggle-more');" class="expander">Need to know more?</h3>
<div id="pp-toggle-more">
<?php
// field-group group id = 293
$parent_fields = get_specifications_fields('293');
foreach ($parent_fields as $pname => $pfield):
$pvalue = $pfield['value'];
$show_parent = false; // Flag to determine if parent should be shown
if (!empty($pvalue)):
$child_fields = get_specifications_fields($pvalue);
foreach ($child_fields as $cname => $cfield):
$cvalue = $cfield['value'];
if (!empty($cvalue)) {
$show_parent = true; // If any grandchild has a value, show parent
break;
}
endforeach;
endif;
if ($show_parent):
?>
<div class="woo-meta-key">
<h4><?php echo $pfield['label'] ?? 'Label not found'; ?></h4>
</div>
<div class="woo-meta-value">
<?php
foreach ($child_fields as $cname => $cfield):
$cvalue = $cfield['value'];
if (!empty($cvalue)): // Check for each grandchild field value
?>
<div class="woo-meta-key">
<strong><?php echo $cfield['label'] ?? 'Label not found'; ?>:</strong>
</div>
<div class="woo-meta-value">
<?php echo $cvalue; ?>
</div>
<?php
endif;
endforeach;
?>
</div>
<?php
endif;
endforeach;
?>
</div>
And here’s what mine looks like. You can see that Bracelet isn’t shown because it doesn’t have any values for this product.

If you use this and enjoy it, please consider a small donation! Every little bit helps.