I’m attempting to add a class to posts when the meta key ‘checkbox’ is clicked. Here’s what I have:
<?PHP $checkbox = get_post_meta($post->ID, 'checkbox', true);
if (!empty($checkbox)){ $has_video = 'icon-has_video'; }
else {$has_video = '';}
?>
<a class='<?php echo $has_video; ?>' href='<?php the_permalink(); ?>'>
… but so far it’s not working. Any suggestions?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
i suggest you check the the value of $checkbox by either checking checkbox under meta_key column in wp_postmeta or *your prefix_postmeta table in your database or simply do var_dump($checkbox); for testing if its 0 or null or an empty string or so then your if condition will not return true and $has_video will not be assigned to ‘icon-has_video’.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0