我正在尝试使用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;
}
它用于获取值,因此,如果行已输入,则所有内容都会正确显示,但是,当行为空时,我似乎无法弄清楚如何隐藏整个内容。
当前即使行为空,它仍然显示列表。
我在这里做错了什么?
在具有行的函数上添加计数:如果由于某种原因返回的空集为0,则可以将计数增加到1。