简体   繁体   中英

How to hide empty custom fields in Wordpress?

I've got div's like this:

<div class="price"> 
   <p>Price: <?php echo get_post_meta($post->ID, 'Price', true); ?></p>  
</div>
<div class="m2">
   <p>m^2: <?php echo get_post_meta($post->ID, 'm2', true); ?></p>
</div>
<div class="year">
   <p>Year: <?php echo get_post_meta($post->ID, 'Year', true); ?></p>  
</div>
<div class="rooms">
   <p>Room #: <?php echo get_post_meta($post->ID, 'Rooms', true); ?></p>
</div>  

I want to hide empty fields, but my solution doesn't works:

<?php if( get_field('Price') ): ?>
    <p>Price ($): <?php the_field('Price'); ?></p>
<?php endif; ?>

Use below code to only print the Price line when the field Price is not empty:

<div class="price"> 
   <?php if (!empty(get_post_meta($post->ID, 'Price', true))) { >?
       <p>Price: <?php echo get_post_meta($post->ID, 'Price', true); ?></p>  
   <?php } >?
</div>

Do the same for all other lines.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM