If you’ve already changed the code in your template file, the code in your template looks something like this:
<p><strong>My Current Mood is:<strong> <em><?php $key="mood"; echo get_post_meta($post->ID, $key, true); ?></em></p>
To make WordPress check to see whether the mood
Custom Field exists, add this code to the line above your existing code:
<?php if ( get_post_meta($post->ID, 'mood', true) ) : ?>
Then add this line of code to the line below your existing code:
<?php endif; ?>
Put together, the lines of code in your template should look like this:
<?php if ( get_post_meta($post->ID, 'mood', true) ) : ?>
<p><strong>My Current Mood is:</strong> <em><?php $key="mood"; echo get_post_meta($post->ID, $key, true); ?></em></p>
<?php endif; ?>
The first line is an IF
statement that asks “Does the mood
key exist for this post?” If so, the value gets displayed. If not, WordPress skips the code, ignoring it so that nothing gets displayed for the mood
Custom Field. The final line of code simply puts an end to the IF
question.3