Hi John,
That’s great. Your solution is all working for me now, there was another typo so here is the code again if anyone else is looking for a similar solution.
Thanks again for your help. Really appreciated.
Kind Regards
Paul
<?php
$group = 'burial_census_information';
$sub_fields = array(
'burial_1841_census',
'burial_1851_census',
'burial_1861_census',
'burial_1871_census',
'burial_1881_census',
'burial_1891_census',
'burial_1901_census',
'burial_1911_census',
'burial_1921_census',
'burial_1939_census'
);
$has_content = false;
foreach ($sub_fields as $sub_field) {
if (get_field($group.'_'.$sub_field)) {
$has_content = true;
${$sub_field} = get_field($group.'_'.$sub_field);
}
}
if ($has_content) {
?>
<div class="container-census-information burial-container">
<h2>Census Information this should only appear if there is content in any of the sub_fields</h2>
<?php
foreach ($sub_fields as $sub_field) {
if (!empty(${$sub_field})) {
?>
<h3><?php echo substr($sub_field, 7, 4); ?></h3>
<p><?php echo ${$sub_field}; ?></p>
<?php
}
}
?>
</div>
<?php
} ?>
Hi John, Thanks so much for your full and helpful answer, including the link to the variable variable documentation. Much appreciated. I think there may be an error on line 23 with the braces?
if ($has_content} {
Not sure if this should be:
if {$has_content} {
OR
if ($has_content) {
Many thanks
Paul
Hi ichabod,
Thanks for your comments. The code doesnt work because the group returns true when it has subfields even if they are empty. Im thinking I need to be using an array and checking that but I cant seem to get that to work either (my error im sure).
Yes this would probably work better as a repeater but I have lots of other groups and fields where that is not the case which will require the same solution so I need to figure this out.
Any help would be greatly appreciated.
Kind Regards
Paul
I am having the same issue on 5.8.11 with select fields returning an array when default value set but no value selected.
I should add, I meant the container div should only appear when a sub_field in a group has content. I can easily echo html with the sub_field content but havent worked out how to successfully add a container div around all of the content shown in the code snippet above. When adding any div html it shows regardless of whether the sub_fields do or do not have content.
Hope you can help. Bit confused. ta.
Paul
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.