Support

Account

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

  • 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 
    } ?>