For me, WordPress is the best CMS I have ever came across. in 2015 it powers about 23% of the websites in the world.
It’s easy to use, flexible and can be modified in any way. The WordPress has gained more popularity because of its custom fields. Before WordPress custom fields were introduced WP was just a blogging platform. After the introduction of custom fields, it has been widely used as Content Management system. So how to use custom fields? Let me first give you little introduction to custom fields.
What are WordPress Custom Fields?
Custom Field in WordPress are additional data you can add to your posts and pages. For a good start you can check “Custom Fields”. It’s a basic tutorial but will give you little idea about custom fields. The page doesn’t contain much use of custom fields that’s why many people don’t know what to do with it, but the truth is, you can do much anything you want with custom fields.
The custom fields input is located on “Add Post” or “Add Page” page below your editor. If you don’t see it, click the “Screen Options” and then check the “Custom Fields” box in the Top-Right corner.
There are two different things you can input into a custom field — “Key” and “Value.” It doesn’t get much simpler than that.
What is a Key and Value?
The Key is the name of the custom field and the Value is simply the value you want to give to that name.
For a live example, let’s say you run a blog about medicines.You have categories set up for each post (Ayurvedic, Allopathic, etc.) and you tag your post with manufacturer’s name. But you want to display the Mfg. Date, Expiry Date and Price of the medicine. The best way to add these extra data is using custom fields.
So first click on “Enter New” and you’ll see empty Name and Value. Add Mfg_Date in name field and the value, let’s say “May 2015″ and then click Add Custom Field. Now you have successfully created custom field named Mfg. Date and in future when you create a new post and want to add Mfg. Date, you’ll just have to select it from drop-down and add your value for that post.
Now the question is how to display it? It’s very simple too. In the single.php file of your theme add the following code.
<?php echo get_post_meta($post->ID, “Mfg_Date”, true); ?>
(You might want to display it in ordered list or a paragraph. You can use HTML in the value of the field.)
You can display posts with specific custom field. Let’s go to our Medicine. Now what if I want to display list of medicine with Mfg. Date? This can be achieved by the following code.
If we want to display medicine manufactured in August 2015, we could do something with the following code:
This is general tutorial, now it depends on your imagination, how you use it. Now, here is an interesting bit for everyone, who is running WordPress blog for long time. How many times, you have changed your theme and use WordPress custom field to Add images or any other element? How many times you have added a new plugin like Popularity count, which again adds new custom field. Have you imagined what happens to custom field, when you change your theme or remove plugins?
Do let me know if you have more to share on WordPress custom fields and how to use it effectively?