Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using, for example: <?php echo get_post_meta($post->ID, "Pricing_Land_Only", true); ?> but need to know how to implement the 'else' function to display 'Nothing found' if I didn't type anything in that field.

The reason being is for different pricing fields that I have and while not all pages use the same fields, most would. Thanks!

share|improve this question
add comment

1 Answer 1

You could do it like this:

// store post meta in a variable and use that so you aren't pulling the post meta data twice
$landPricing = get_post_meta($post->ID, "Pricing_Land_Only", true)
if(!empty($landPricing))
{
    echo $landPricing;
}
else
{
    echo "Sorry, nothing was found.";
}
share|improve this answer
    
why not just store everything in a variable, then check the variable instead of calling get_post_meta twice? –  Brian Fegter Oct 11 '12 at 22:56
    
For some reason, WP didn't like me doing that, or I added it incorrectly: Fatal error: Can't use function return value in write context in /home/toasterd/public_html/bctderezzed.co/wp-content/themes/trece/tour-template.‌​php on line 83 when entered as <?php if(!empty(get_post_meta($post->ID, "Pricing_Land_Only", true))) { echo get_post_meta($post->ID, "Pricing_Land_Only", true); } else { echo "TBD or not applicable"; } ?> –  toasterdroid Oct 11 '12 at 22:58
    
+1 for the edit. –  Brian Fegter Oct 11 '12 at 23:30
    
Thanks @BrianFegter, I definitely wasn't thinking very clearly when I wrote that out. toasterdroid please see my updated answer, as I think what I originally showed you would break as you have seen. –  Ben Oct 11 '12 at 23:46
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.