WordPress For Dummies, 8th Edition
Book image
Explore Book Buy On Amazon
Once you’ve added the necessary code to your template file to display your Custom Field, you should be ready to publish your WordPress site. But what if you want to publish a post in which you don’t want the mood Custom Field to appear? You can easily make WordPress check to see whether the Custom Field is added. If it finds the Custom Field, WordPress displays your mood; if it doesn’t find the Custom Field, WordPress doesn’t display the Custom Field.

If you’ve already changed the code in your template file, the code in your template looks something like this:

My Current Mood is: ID, $key, true); ?>

To make WordPress check to see whether the mood Custom Field exists, add this code to the line above your existing code:

ID, 'mood', true) ) : ?>

Then add this line of code to the line below your existing code:

Put together, the lines of code in your template should look like this:

ID, 'mood', true) ) : ?>

My Current Mood is: ID, $key, true); ?>

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

About This Article

This article is from the book:

About the book author:

Lisa Sabin-Wilson is cofounder of WebDevStudios, one of the largest WordPress design and development agencies in the world. She is a regular public speaker at national events on topics such as WordPress, development, design, CSS, and social media.

This article can be found in the category: