0

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!

1 Answer 1

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.";
}
3
  • why not just store everything in a variable, then check the variable instead of calling get_post_meta twice? Oct 11, 2012 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"; } ?> Oct 11, 2012 at 22:58
  • 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. Oct 11, 2012 at 23:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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