Search code examples
phpwordpressif-statementis-empty

Shortest way to output 0 if meta does not have value / is empty


Hope that title makes sense, I have a few meta fields that I want to output 0 if they are empty.

I'm currently outputting the values with this:

<?php meta('some-field'); ?>

Edit: the above code will echo out the value


Solution

  • For example use the ternary operator. After PHP 5.3:

    echo meta('some-field') ? : 0;
    

    Before PHP 5.3:

    echo meta('some-field') ? meta('some-field') : 0;