我正在使用高級自定義字段(ACF)。如果該字段爲空,我不想顯示ACF中繼器字段the_sub_field('feature_image_post');
。如果字段爲空,我將字段封裝在div中,並使用CSS編寫了display = none,但我確信有更好的方法來完成此操作。我的邏輯似乎不起作用。如果字段爲空,如何創建不顯示字段的條件?
<?php
// check if the repeater field has rows of data
if(have_rows('repeat_field')):
// loop through the rows of data
while (have_rows('repeat_field')) : the_row();
// display a sub field value
echo '<span class="place-name">';
the_sub_field('place_name');
echo '</span>';
if (!empty (get_sub_field('feature_image_post'))) {
echo '<div class="post-feature" style="display:block;">';
echo the_sub_field('feature_image_post');
echo '</div>';
}
else {
echo '<div class="post-feature" style="display:none;">'
echo the_sub_field('feature_image_post');
echo '</div>';
}
endwhile;
else :
// no rows found
endif;
絕對是一個好點!我會看一看 – Mariton