WordPress Custom Field Template

One of the best feature of wordpress is its custom field. It lets you add additional text or code aside from the article. It can be another info such as author name, extra thumbnail or image, video code, and more.

You can do a lot of things using this amazing feature. Here are some useful codes that you can use.

How to Add Custom Field

First you need to make sure that you enable it on the screen options button at the top of the page while creating a post or page. After that, you can enter a new custom field name and it’s value.

how to add custom field in post or page

Now let’s try to be realistic. Here’s a real example from a real website. This website use a post with a YouTube video before the article. Instead of combining the video code to the article, I decided to use custom field to organize the text and video.

custom field video code

In creating custom field names, make sure to avoid spaces to avoid different issues. I would recommend to use underscores instead.

Get Custom Field Value and Display it

The next question is how am I going to get custom field value on my customized WordPress template. The answer is below:

Display [Something] if Custom Field does not Exist

The code above will display the value of the video_code but it will display nothing if our value is empty. Here’s a code if you want to display something if custom field does not exist.

In the case above, an image will display if the video code is not provided by the author of the article. You can also replace it with text or anything you want.