How can I echo this function: <?php echo "€" . get_post_meta( get_the_ID(), 'deal_sale_price', true ); ?> PHP: only if 'deal_sale_price' has a value? Thanks a lot.
Since this is a WP question, I'm not 100% sure this will work, but it might: <?php if (!empty(get_post_meta(get_the_ID(),'deal_sale_price',true))) { echo "€" . get_post_meta( get_the_ID(), 'deal_sale_price', true ); } ?> PHP:
for readability, if you aren't too confident with PHP, you could go with this version of @PoPSiCLe's code <?php $deal_sale_price = get_post_meta(get_the_ID(),'deal_sale_price',true); if (!empty($deal_sale_price)) { echo "€" . $deal_sale_price; } ?> Code (markup):
Not confident with PHP at all, just starting to learn bits of it actually. Anyway Thanks a lot guys, both worked
Legibility nothing, if you're gonna call a procedure more than once, STORE THAT PUPPY! I'd also advise against the string addition, "curly brackets for nothing", double quotes on a non-parse string.. and you could do the assignment and the check as a single operation. <?php if (!empty( $deal_sale_price = get_post_meta(get_the_ID(),'deal_sale_price',true) )) echo '€', $deal_sale_price; ?> Code (markup): Remember, comma delimited echo is faster than string addition, uses less memory, and actually will run in the order you coded it. Just saying... goes with my general belief that 99% of the time people use string addition or double quotes on a string, they're doing something WRONG.
<?php function Check( $value ) { if ( strlen($value) && is_string($value) ) { return true; } else { return false; } } if Check(get_post_meta( get_the_ID(), 'deal_sale_price', true )) echo get_post_meta( get_the_ID(), 'deal_sale_price', true ); ?> Code (markup):
... and one with an if statement at that... Must just be a chubby chaser with a love of bloat like that.