Home › Forums › General Issues › Displaying div only when sub_field in group has content › Reply To: Displaying div only when sub_field in group has content
Yes, a group field always returns true using have_rows() you need to test the sub fields.
The following uses variable variables to reduce multiple ifs into a loop https://www.php.net/manual/en/language.variables.variable.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
}
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.