Support

Account

Home Forums General Issues Check if subfield of repeater has any value, before the_row Reply To: Check if subfield of repeater has any value, before the_row

  • This is where output buffers come in handy, https://www.php.net/manual/en/book.outcontrol.php. I won’t repeat your entire code here, just the basics.

    
    <?php 
      // only send output if there is content
      $has_content = false; // a flag to output content
      ob_start(); // start output buffering
    ?>
    <p>this content will be buffered and only output if the repeater has values.</p>
    <?php 
      if (have_rows('repeater field')) {
        while (have_rows('repeater field')) {
          the_row();
          if (get_sub_field('sub field name')) {
            // row has content
            // output the sub field
            the_sub_field('sub field name');
            // set flag to output content
            $has_content = true;
          }
        }
      }
      
      // get the content of the buffer and clear it
      $content = ob_get_clean();
      // if the flag was set then output the content
      if ($has_content) {
        echo $content;
      }