我想我应该简化我的问题。 这可能是一个 PHP 问题,所以我提前道歉,因为这可能不是 ACF 问题,但可能是我对 PHP 了解不够的问题,我不确定为什么这不起作用。
因此,我正在使用 ACF 中继器字段,并且我想在该字段中未输入任何数据时隐藏中继器子字段。
<?php
// check if the repeater field has rows of data
$feature_posts = the_sub_field('feature_image_post');
if( have_rows('repeat_field') ):
// loop through the rows of data
while ( have_rows('repeat_field') ) : the_row();
// display a sub field value
echo '<div style="float:left">';
the_sub_field('restaurant_name');
echo '</div>';
echo '<div style="float:left">';
the_sub_field('restaurant_state');
echo '</div>';
echo '<div style="float:left">';
the_sub_field('restaurant_image_post');
echo '</div>';
if (empty($feature_posts)) {
echo '<div style="display:none">';
the_sub_field('feature_image_post');
echo '</div>';
}
else {
the_sub_field('feature_image_post');
}
endwhile;
else :
// no rows found
endif;
?>
最佳答案
使用 get_sub_field
添加条件语句来检查字段是否为空。 the_sub_field
回显该值,get_sub_field
返回该值,因此它可以在条件语句中使用或存储在变量中。
if( get_sub_field('restaurant_name') != "" ) {
echo '<div style="float:left">';
the_sub_field('restaurant_name');
echo '</div>';
}
关于javascript - 如何使用 PHP 隐藏空的 ACF 字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44371163/