2017-06-05 8 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>'; 
} 
関連する問題