I need opposite of this:
<?php if ( get_post_meta($post->ID, 'price_list_category1', true) ) : ?>style="display:none;"<?php endif; ?>
In other words I want style="display:none;"
only when meta data doesn’t exist.
I thought it would be straightforward like if ( get_post_meta($post->ID, 'price_list_category1', true
but this true/false turns out to be a completely different stuff.
any ideas?
Thank you.
4 s
You could use the empty
function inside your if
as such :
<?php if( empty( get_post_meta( $post->ID, 'price_list_category1', true ) ) ) : ?>style="display:none;"<?php endif; ?>
The above returns an error, you should assign the return value to a variable. See my edit below.
Warning
empty
might not be the best option depending on the values you store in the meta. Values like false
, 0
etc… will be considered empty.
Check the PHP manual for the full list of values that are considered empty.
Edit
You can try assigning the meta to a variable, and using that in the if
statement.
<?php
$price_list = get_post_meta( $post->ID, 'price_list_category1', true );
?>
And then…
if( empty( $price_list) ) : ?>style="display:none"<?php endif; ?>