Wordpress元值为空不显示

Wordpress元值为空不显示,wordpress,post,Wordpress,Post,我使用的是目录主题,我使用的代码如下: <?php $listing_contact = get_post_meta( get_the_ID(), 'listing_contact', true ); echo $listing_contact; ?> <?php if( empty( $listing_contact) ) : ?>no contact<?php endif; ?> 没有联系 但是,一旦$listing\u contact为空,“无联系

我使用的是目录主题,我使用的代码如下:

<?php $listing_contact = get_post_meta( get_the_ID(), 'listing_contact', true ); echo $listing_contact; ?>
<?php if( empty( $listing_contact) ) : ?>no contact<?php endif; ?>

没有联系

但是,一旦
$listing\u contact
为空,“无联系人”值就不会显示。有人能帮我解决这个问题吗?

下面将检查
$listing\u contact
是否有任何值,如果它没有显示任何内容,如果它确实显示了
$listing\u contact
的内容

<?php
$listing_contact = get_post_meta( get_the_ID(), 'listing_contact', true ); 
if(!($listing_contact == null || $listing_contact == '')){
    echo $listing_contact; 
}else{
    echo "no contact";
}
?>


请您尝试上面的代码好吗?

您的代码可以正常工作,尽管我建议您重新编写:

<?php $listing_contact = get_post_meta( get_the_ID(), 'listing_contact', true ); ?> 

<?php if ( empty( $listing_contact ) ) : ?>
   No contact
<?php else : ?>
   <?php echo $listing_contact;
<?php endif; ?>

没有联系

也许可以检查您是否在
get\u ID()
中获得了正确的ID,或者您是否在条件中实际查找了
empty()

@festinger…您的代码应该可以工作..一开始没有问题…如果没有得到正确的结果,请检查get\u ID()。。。