2017-06-05 97 views
1

我想我會簡化我的問題。 這可能是一個PHP問題,所以我先進的道歉,因爲這可能不是一個ACF問題,但它可能是一個問題,我不知道足夠的PHP,我不知道爲什麼這不起作用。你如何使用PHP隱藏一個空的ACF字段?

所以我使用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; 

    ?> 

回答

1

使用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>'; 
} 
相關問題