Theme Development, WordPress

How To Add WordPress Custom Field To Any Post

Custom fields are important to elaborate your WordPress post as it enables you to add extra data. WordPress custom field also helps you to add  extra features and functionalities and specific information in individual posts.

By adding custom fields you can edit your WordPress pages and post metadata so you can display whatever you want in your website. By default the custom field option is hidden and disable in WordPress. But you can easily enable or use plugins to refine the feature more useful.

Here in this article I will show you how you will enable and add WordPress custom field in your theme.


How to enable WordPress custom field

To enable a custom field you need to open up a post. You can create a new post or open up an old post whatever you want. Open up any post click on the Screen Option  tab. This tab will appear in the right corner of the page.


After clicking the screen option you can see the screen element list. You can select or deselect any element that you want to add or delete from this particular post.. By default the custom field option is unchecked. So you need to check the custom field option and close the screen option.


Now scroll down below to the post and you will see your new custom field option



So here in under name and value you can add metadata to describe the information you want to display in this post. You can find some metadata in the name option which is dependent on your themes and installed plugins.

Choose your metadata and add value in the value option. When all done click on the Add Custom Field  button and save your update.


Add Conditional Code to The Theme File

Following the previous step we will be able to add specific metadata to any post. But now we need to direct our website so that it knows what to do.  Here we need a little bit of coding. But it’s very easy so you don’t need any developer to do it. Without any coding knowledge you can easily implement it in your theme.

Open up your WordPress dashboard and click on Appearance >Edit option. Here you need to add some code to the file which makes up your website. Look for the  single.php  file on the right sidebar.

Open the file and this additional code which will direct your website what to do with your new custom field.

Suppose you add a custom field metadata in your post to show whether the sponsor or nor then then the code will be like this.


$meta = get_post_meta( get_the_ID(), 'Sponsored Post' );
if( $meta[0] == 'Yes' ) {
<p>This post is sponsored content, and we received a free copy of the product in order to conduct our review.</p>
<?php } ?>



This little code checks whether the post has sponsored  custom field or not. If found a sponsor custom field then check the value yes or no. If the value is yes then it will show the info to that particular post. And if there is no custom field and the value is no then it will show nothing.


You Might Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *