How to Display the Sub-Field of a Group in a PHP Template
In Advanced Custom Fields (ACF), if you want to display the subfield of a group in your PHP template, you’ll need to use the get_field function. Here’s a general example assuming you have a field group named “my_group” with a subfield named “sub_field”:
// Assuming your post ID is available, or use get_the_ID() for the current post
$post_id = get_the_ID();
// Get the value of the subfield
$sub_field_value = get_field(‘my_group_sub_field’, $post_id);
// Display the subfield value
echo $sub_field_value;
?>
[/php]
Make sure to replace “my_group_sub_field” with the actual name or key of your subfield.
If your subfield is nested within a repeater or a flexible content field, you would need to navigate through the field structure accordingly. For example, if your subfield is within a repeater called “my_repeater” and the subfield is “sub_field”, you would do something like this:
$post_id = get_the_ID();
// Get the repeater field
$repeater_field = get_field(‘my_repeater’, $post_id);
// Loop through the repeater rows
if ($repeater_field) {
foreach ($repeater_field as $row) {
// Get the value of the subfield within the repeater
$sub_field_value = $row[‘sub_field’];
// Display the subfield value
echo $sub_field_value;
}
}
?>
[/php]
Adjust the field names according to your actual ACF field structure. If you’re unsure about the field names or keys, you can find them in the ACF Field Group settings in the WordPress admin panel.