2013-10-19 52 views
0

在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; ?> 

这最后一部分只是为了检查自己的价值。

我不知道我哪里出错了。

+0

我个人不喜欢strcasecmp。我会使用'strtolower($ feat)=='yes''作为我的比较 –

+0

@cale_b Yeahhh我'有这个字符串比较的东西的大量问题。我在页面上有两个字符串比较,并且都被打破。我试过$ feat ==='是'和strcmp($ feat,'yes')都不起作用。 –

+0

那么'var_dump($ feat)'输出什么?你应该使用var_dump而不是echo来查看变量的内容 - 它会给你变量的类型和其他有用的信息。 –

回答

2

你的HTML不裹PHP,因此不受该条件语句

变化

<?php if(strcasecmp($feat, 'yes') == 0)?> 
            <a href=""><div class="featured_reject">Featured Rejection</div></a> 

           <?php endif; ?> 

<?php 
    if(strcasecmp($feat, 'yes') == 0){ 
     echo "<a href = ''><div class = 'featured_reject'>Featured Rejection</div></a>" 
    } 
?> 
+1

或者我的首选是实际上把:在声明的末尾(或用{}包装) – jdog

+0

这是正确的答案。出于某种原因,我认为它会把它作为一个包装器来处理?php if和endif,并在它之间执行代码,如果它是真的。谢谢。 –

+0

@帕特里克考利 - 它会的。你理解正确。我怀疑在'if'行结尾缺少大括号或冒号。 –

1

如果PHP的语法.. endif是:

if (condition): 
    ... 
endif; 

(按:http://php.net/manual/en/control-structures.alternative-syntax.php

所以,你需要改变

<?php if(strcasecmp($feat, yes) == 0)?> 
    <a href=""><div class="featured_reject">Featured Rejection</div></a> 
<?php endif; ?> 

到(请注意多:==后)if语句:

<?php if(strcasecmp($feat, yes) == 0):?> 
    <a href=""><div class="featured_reject">Featured Rejection</div></a> 
<?php endif; ?> 
+0

我刚刚看到了由jdog给劳埃德银行答复的意见。我认为我唯一的反应将是“他说的......” – Marc