-1

I am trying to create a TO DO list with ACF Advanced Custom Fields in Wordpress.

What I want to achieve is a shortcode that will display the TO DO repeater wrapped in Div tags with H3 title.

But IF the sub-fields are empty, nothing should show up, not even the H3 title.

I got as far as this:

add_shortcode( 'TO-DO-LIST', 'my-to-do-list');  
function my-to-do-list($atts){ 

      if(!function_exists('get_field'))
        return;

      ob_start();
      // check if the repeater field has rows of data
      if( have_rows('acf_to_do_repeater_group') ):

          echo '<div id="to-do-list-wrapper">';
          echo '<h3>TO DO:</h3>';
          echo '<div class="to-do-content-wrapper">';
          echo '<ul class="to-do-list-wrap">'; 
          ?>
                <?php
                // loop through the rows of data
                while ( have_rows('acf_to_do_repeater_group') ) : the_row();
                     // display a sub field value
                     $content = get_sub_field('to-do-repeater-subfield'); 

                     echo '<li>' . $content . '</li>';  
                endwhile;

           echo '</ul></div></div>';         
       endif;
    $output = ob_get_clean();
    return $output;
} 

It works for getting the values, so if the rows have input, everything shows up correctly, however I can't seem to be able to figure out how to hide the entire thing when the rows are empty.

Currently even if the rows are empty, it still shows the list.

What am I doing wrong here?

Joe Bloggs
  • 1,410
  • 2
  • 24
  • 53

1 Answers1

0

Add a count on the have rows function: if its returning a empty set for some reason for 0, you can increase the count to 1.


if( have_rows('acf_to_do_repeater_group') && count(have_rows('acf_to_do_repeater_group')) > 0):
KGreene
  • 790
  • 7
  • 11