简体   繁体   中英

ACF Repeater field [shortcode] - do not show anything if subfields are empty

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?

在 have rows 函数上添加一个计数:如果由于某种原因返回一个空集为 0,您可以将计数增加到 1。


if( have_rows('acf_to_do_repeater_group') && count(have_rows('acf_to_do_repeater_group')) > 0):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM