WordPress: Display Multiple Custom Fields Conditionally

In Wordpress Tutorials by Laura

Displaying custom fields on your wordpress site can be really powerful. You can use these to display metadata about the post from what music is playing in the background to the type of food you are eating.

Custom fields are really just one way of associating miscellaneous metadata with your posts. Custom fields have of two main parts, a key and a value. The key is the name of the custom field, and the value is its ‘content’. I recently saw a travel blogger using custom fields to display location. There is no limit on how many custom fields you want to display and they can store custom CSS, URLs, and even embed codes.

When you go to add your first custom field you will need to make sure that the custom fields box is check on the top of your screen under screen options.

wp custom field

Then scroll down to the custom fields section and add the field you want to display. You will need to click “Enter New” to enter the key for the field you want to add.


Once you enter the key “My Soundtrack” it will show up in future posts in a dropdown menu.


All of that is pretty straightforward but now in order to get the custom fields to show up on your post you will need to edit the php files in your theme. Be sure to use a child theme so that these edits do not get overwritten when you update your theme. You will be editing the single.php file in your theme.

I used this function to show years when couples from camp both met and married as well as which camps they attended. I added this code right under “the_title();” in the single.php file. You can see that because I have multiple fields the variables are given unique names for each field. I am also enclosing each field in an “if statement” so that the title will only show up if there is content within the field.

Just add the code below and your fields will show up on your post!

Screen Shot 2014-01-07 at 8.41.28 PM