ACF 中继器字段 [简码] - 如果子字段为空,则不显示任何内容



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

我想要实现的是一个短代码,它将显示包裹在带有 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;
} 

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

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

我在这里做错了什么?

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


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

最新更新