我有以下代码:
<?php $buycheck = get_post_meta($post->ID, 'buy-link', true); ?>
<?php if ( $buycheck ) : ?>
<div class="section-title sidebar span5">
<h5>Get This Release</h5>
</div>
<?php else : ?>
<div class="section-title sidebar span5">
<h5>More Releases</h5>
</div>
<?php endif; ?>
稍后在我的代码中,我希望能够说,如果购买链接不存在,即该字段中没有数据,那么就做一些事情,否则就做一些不同的事情。
不知道怎么做!感谢帮助!
(顺便说一句,我先把这个问题发布到Wordpress Stack Exchange。它在那里被投票关闭,因为它显然比Wordpress更关心PHP布尔逻辑。)https://wordpress.stackexchange.com/questions/60387/how-do-i-do-if-post-meta-does-not-exist#comment78412_60387)
<?php if($buycheck ==''){ /*stuff*/ } ?>
这将呈现$buycheck,如果它为空,则==等于''无。
您可以设置一个全局变量,稍后可以检查buylink是否存在:
<?php
$buycheck = get_post_meta($post->ID, 'buy-link', true);
$GLOBALS['buy_link_exists'] = !empty($buycheck);
?>
<?php if ( $buycheck ) : ?>
<div class="section-title sidebar span5">
<h5>Get This Release</h5>
</div>
<?php else : ?>
<div class="section-title sidebar span5">
<h5>More Releases</h5>
</div>
<?php endif; ?>
然后稍后在您的代码中:
<?php if ($GLOBALS['buy_link_exists'])): ?>
it exists, do one thing
<?php else: ?>
it does not exist, do something else
<?php endif; ?>
如果需要实际值,可以设置一个包含get_post_meta
返回值的全局,以便使用实际值。