[英]How to add CSS class in PHP if/else statement?
I wrote this code to add css class to a post title. 我写了这段代码,将CSS类添加到帖子标题中。 it works in wordpress.
它可以在WordPress中工作。 I think it is correct code but not working
我认为这是正确的代码,但无法正常工作
<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>
but it dont work. 但它不起作用。
Write like this using ternary operator. 使用三元运算符这样写。
$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>
Refer this link for more information https://developer.wordpress.org/reference/functions/get_post_meta/ 请参阅此链接以获取更多信息https://developer.wordpress.org/reference/functions/get_post_meta/
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.