简体   繁体   English

Get_post_meta WordPress

[英]Get_post_meta Wordpress

Need help with a wordpress get_meta_post. 需要有关wordpress get_meta_post的帮助。 I need to display a div only if the custom-field promo is found in the get_meta_post. 仅在get_meta_post中找到自定义字段促销时,才需要显示div。 If true this is suppose to echo the : 如果为true,则应回显:

<?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. 假设这是您的实际代码,您可能会遇到多种错别字,或者对PHP的工作方式有重大误解。 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. 我还将promo后的meta分配给了它自己的变量,因此更容易理解。

You're using get_the_ID wrong. 您使用的是get_the_ID错误。 Get the ID does not accept any parameters and gets the ID of the current post. 获取ID不接受任何参数,并获取当前帖子的ID。 If you need to check if the post has meta 'promo', then just check if get_post_meta returns null/false. 如果您需要检查帖子是否包含元“促销”,则只需检查get_post_meta是否返回null / false。

I'm not sure what you're asking through your example though. 我不确定您通过示例提出的要求。 If you're trying to echo out the post meta: 如果您想回显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>';}?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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