在WordPress中,我使用插件记录元键_featured并添加了yes或no的值。我想添加CSS,如果它是特色,但它是添加div无论结果。PHP中的字符串比较 - 保持返回错误值
<?php if (get_post_meta(get_the_ID(), '_featured', true)) : ?>
<?php $feat = get_post_meta(get_the_ID(), '_featured', true); ?>
<?php if(strcasecmp($feat, yes) == 0)?>
<a href=""><div class="featured_reject">Featured Rejection</div></a>
<?php endif; ?>
<h1><?php echo get_post_meta(get_the_ID(), '_featured', true) ?></h1>
<?php endif; ?>
并不是所有这些都是为了最终目的,有些只是为了测试日志的结果。
<?php if (get_post_meta(get_the_ID(), '_featured', true)) : ?>
这会检查是否有值。工作正常。
<?php $feat = get_post_meta(get_the_ID(), '_featured', true); ?>
记录它作为一个变量
<?php if(strcasecmp($feat, 'yes') == 0)?>
<a href=""><div class="featured_reject">Featured Rejection</div></a>
<?php endif; ?>
这是添加在div的代码。无论值是或不是,它都会添加它。
<h1><?php echo get_post_meta(get_the_ID(), '_featured', true) ?></h1>
<?php endif; ?>
这最后一部分只是为了检查自己的价值。
我不知道我哪里出错了。
我个人不喜欢strcasecmp。我会使用'strtolower($ feat)=='yes''作为我的比较 –
@cale_b Yeahhh我'有这个字符串比较的东西的大量问题。我在页面上有两个字符串比较,并且都被打破。我试过$ feat ==='是'和strcmp($ feat,'yes')都不起作用。 –
那么'var_dump($ feat)'输出什么?你应该使用var_dump而不是echo来查看变量的内容 - 它会给你变量的类型和其他有用的信息。 –