如何在PHP if/else语句中添加CSS类?

如何在PHP if/else语句中添加CSS类?,php,css,class,if-statement,Php,Css,Class,If Statement,我写这段代码是为了在文章标题中添加css类。它在wordpress中工作。我认为这是正确的代码,但不起作用 <h2><a <?php if(get_post_meta(get_the_ID(),'hot',true) == 'on') { ?>class="hottitle" <?php } ?> title="<?php the_title_attribute(); ?>" href="<?php the_permalink() ?

我写这段代码是为了在文章标题中添加css类。它在wordpress中工作。我认为这是正确的代码,但不起作用

<h2><a  <?php if(get_post_meta(get_the_ID(),'hot',true) == 'on') { ?>class="hottitle" <?php } ?> title="<?php the_title_attribute(); ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>


但是它不起作用。

用三元运算符这样写

$key_1_value = get_post_meta(get_the_ID(),'hot',true);
// Check if the custom field has a value.
<h2><a  <?php echo (! empty( $key_1_value )) ? 'class="hottitle"' : '';  ?> title="<?php echo get_the_title_attribute(); ?>" href="<?php echo get_the_permalink() ?>"><?php echo get_the_title(); ?></a></h2>
$key\u 1\u value=get\u post\u meta(get\u ID(),'hot',true);
//检查自定义字段是否有值。
有关更多信息,请参阅此链接



不工作。请看这个:>@kowsaralipour请试试这个>@kowsaralipour我也更新了我的ans请检查。谢谢。但我认为这部分:“‘on’”?“class”不是correct@kowsaralipour请仅在以下情况下使用此选项(获取\u post\u meta(获取\u ID(),'hot',true))。也在ans中更新。没有
bro之类的东西,我每天都在使用它,请检查它的

<h2><a <?php=get_post_meta(get_the_ID(),'hot',true)=='on'?'class="hottitle"':''?> title="<?php=the_title_attribute();?>" href="<?php=the_permalink()?>"><?php=the_title()?></a></h2>