简体   繁体   中英

Get_post_meta Wordpress

Need help with a wordpress get_meta_post. I need to display a div only if the custom-field promo is found in the get_meta_post. If true this is suppose to echo the :

<?php get_post_meta(get_the_ID('promo', true)  
<div class="packagePromoItem">Promotion</div>
?>

Assuming that's your actual code, you have several typos, or major misunderstandings about how PHP works. This should work (using alternative syntax, which I think is a little more readable for this):

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

<?php if ($promo): ?>
  <div class="packagePromoItem">Promotion</div>
<?php endif; ?>

I've also assigned the promo post meta to its own variable so it's easier to follow.

You're using get_the_ID wrong. Get the ID does not accept any parameters and gets the ID of the current post. If you need to check if the post has meta 'promo', then just check if get_post_meta returns null/false.

I'm not sure what you're asking through your example though. If you're trying to echo out the post meta:

<?php if (get_post_meta(get_the_ID(), 'promo', true))) { echo'<div          
class="packagePromoItem">' . get_post_meta(get_the_ID(), 'promo', true) .   
'</div>';}?>

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