【问题标题】:ACF Repeater field [shortcode] - do not show anything if subfields are emptyACF 中继器字段 [短代码] - 如果子字段为空,则不显示任何内容
【发布时间】:2020-05-15 12:58:46
【问题描述】:

我正在尝试在 Wordpress 中使用 ACF 高级自定义字段创建一个 TO DO 列表。

我想要实现的是一个短代码,它将显示包含在带有 H3 标题的 Div 标签中的 TO DO 中继器。

但如果子字段为空,则不会显示任何内容,甚至是 H3 标题。

我已经做到了:

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

它用于获取值,所以如果行有输入,一切都会正确显示,但是当行为空时,我似乎无法弄清楚如何隐藏整个东西。

目前即使行为空,它仍然显示列表。

我在这里做错了什么?

【问题讨论】:

    标签: php wordpress advanced-custom-fields repeater acfpro


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-01-04
      • 2019-04-25
      • 2018-07-22
      • 2018-04-11
      • 2017-01-01
      • 2022-08-18
      • 1970-01-01
      • 2016-12-07
      • 2018-02-10
      相关资源
      最近更新 更多