PHP, Wordpress

WordPress – How to use Custom Fields

If you are like me who gets confuse on how to use the Custom Fields, then this is for you.  This is very useful if you know how to use it.

You can create and add your own meta data on a page or a post using.

In my case, I have a template that shows the archive post like a listing type.

But I wanted to add a Business Name entry too. My template doesn’t have this option so I will be using Custom Fields to do it.

On a page/post, scroll down to the Custom Fields section

Enter the custom field name and value that you would like to add, like below:

and save the Post/Page.

 


 

To display the custom field info we just added, we need to update our template where we want the data displayed. In my case, I want to add it on my archive page. I have added the code after the Location text.

$business_name = get_post_meta($post->ID, 'business_name', true); 
// This line declares the business_name variable for me to be able to check if the custom field of that name is available and is not empty on the backend..

if ($business_name) {
echo '<span><strong>'.esc_html__('Business Name:').'</strong>' . get_post_meta($post->ID, 'business_name', true);
}
// If the business_name custom field is value is filled, it will display the text and value of the custom field as stated on the above code.

Use whatever custom field name that you like and replace on the sample code above.