Support

Account

Home Forums General Issues Displaying div only when sub_field in group has content

Solved

Displaying div only when sub_field in group has content

  • Hi,

    I wonder if you could help? I wish to have a div appear only when the sub fields in a group have content. Currently my code is showing the div regardless of whether there is content in the sub_fields. This is my code, please could someone point me in the right direction, having real troubles googling this and finding an answer.

    Many thanks
    Paul

    <?php if( have_rows('burial_census_information') ): ?>
    <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 while( have_rows('burial_census_information') ): the_row(); 
     				 				                   
    $census1841 = get_sub_field('burial_1841_census');
    $census1851 = get_sub_field('burial_1851_census');
    $census1861 = get_sub_field('burial_1861_census');
    $census1871 = get_sub_field('burial_1871_census');
    $census1881 = get_sub_field('burial_1881_census');
    $census1891 = get_sub_field('burial_1891_census');
    $census1901 = get_sub_field('burial_1901_census');
    $census1911 = get_sub_field('burial_1911_census');
    $census1921 = get_sub_field('burial_1921_census');
    $census1939 = get_sub_field('burial_1939_census');
    				
    if ($census1841) { 
    echo "<h3>1841</h3><p>$census1841</p>";
    }
    if ($census1851) {
    echo "<h3>1851</h3><p>$census1851</p>";
    }
    if ($census1861) {
    echo "<h3>1861</h3><p>$census1861</p>";
    }
    if ($census1871) {
    echo "<h3>1871</h3><p>$census1871</p>";
    }
    if ($census1881) {
    echo "<h3>1881</h3><p>$census1881</p>";
    }
    if ($census1891) {
    echo "<h3>1891</h3><p>$census1891</p>";
    }
    if ($census1901) {
    echo "<h3>1901</h3><p>$census1901</p>";
    }
    if ($census1911) {
    echo "<h3>1911</h3><p>$census1911</p>";
    }
    if ($census1921) {
    echo "<h3>1921</h3><p>$census1921</p>";
    }
    if ($census1939) {
    echo "<h3>1939</h3><p>$census1939</p>";
    }
    
    endwhile; ?>
    </div>
    <?php endif; ?>
  • 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

  • This demonstrates how to display the contents of a group field PrepaidGiftBalance.

  • it looks like it should already be doing that. Because if you don’t have any sub fields field in then the have_rows(‘burial_census_information’) would be empty. Don’t create the row if you don’t have content to fill in?

  • I’m wondering if it’ll work better as a repeater

  • 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

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

  • Yep, that was a typo. Happens when I try to code in a text field.

  • 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 
    } ?>
Viewing 10 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic.