How to add CSS class in PHP if/else statement? -
i wrote code add css class post title. works in wordpress. think correct code 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 dont work.
write using ternary operator.
$key_1_value = get_post_meta(get_the_id(),'hot',true); // check if custom field has 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 link more information https://developer.wordpress.org/reference/functions/get_post_meta/
Comments
Post a Comment