What are Custom Fields and how do they work

While you were writing your previous article, you might have scrolled over Custom Fields area in the post editor. This simple, yet so powerful WordPress feature, might go unnoticed by beginners but it’s actually a feature which can undoubtedly transform your simple and plain website into a dynamic portal which will attract new visitors.

What are exactly custom fields? These little fields allow any user to enter extra information about the post. This is known as meta-data, i.e. “data about data” and allows you to attach additional information to your posts. Values you can enter in custom fields can range from something simple as adding a text or a number to your post, adding images, changing styles through the field, or actually doing anything that goes through your mind.

Custom fields

Maybe everything will be clearer with an example.

Imagine that you’re running a website dedicated to video games. You write news and publish trailers, play around with gaming hardware so you can talk about it and of course you write reviews after new games arrive. After you have written a review and placed screenshots and gameplay videos in the article, you probably want to display important information about the game.

Instead of hiding most important information about a game in the lengthy text, you might want to display game’s score, developer and publisher names linking to their website, release date, etc. Yes, custom fields are something that can help you do the magic.

If you let your authors enter that information in a custom field, you can easily extract the information and display it in addition to the review. You’ve seen info boxes on review sites. If you do so, all reviews can have a standardized template – this can improve user experience, both on the front and the backside.

Following the gaming website example, you would end up having several custom fields which your authors can enter and which you show to your readers:

Call of Duty info

This is the information your authors would enter into custom fields, and you can display the same into a post. But how?

Display all custom fields:

If you only have one field or you want to show all of your fields on the same spot, you can hook you meta-data all at once with a simple function:

  1. Open single.php
  2. Find the_content () function and paste this code before or after it (depending on where you want to display custom fields):
  3. <?php the_meta(); ?>
  4. Save changes

Display specific custom field:

That’s it. That simple function will now stay hooked and it will show all custom fields from a post. But what if you want to show different field in a different position or if you only want to show one specific key? Then you will use slightly different approach and define the key which will be displayed:

  1. Open single.php file
  2. Find the content and c/p the following:
  3. <?php echo get_post_meta($post->ID, 'key', true); ?>
  4. Replace ‘key’ with the actual custom field you are using in a post. For example: ‘Publisher’
  5. Save changes

This function will check each and every post in the loop and search for a custom field named “Publisher”. If the key was found, its value will be displayed. You can further customize the way a custom field is being displayed by adding a class to it and style the class with CSS or you can directly enter HTML into the field value.

Conditional Custom Fields:

If you want to, you can use custom fields as conditional. Let stay with the example and let’s say there are times that you won’t be able to know a release date of the game you’re writing about. Instead of typing a custom field, you can automatize the process and tell WordPress to write “TBA” (to be announced) into Release Date field if there isn’t any data added by the user:

<?php $release_date = get_post_meta($post->ID, 'Release Date', true);
if ($release_date) {
?>
<?php echo $release_date; ?>
<?php } else { ?>
<p>To Be Announced.</p>
<?php } ?>

OK, we’ll wrap it up from here. This is the most basic way of using custom fields. But now that you have covered the basics, you can actually do pretty much anything with them. You can, for example, show your mood or a song you’re listening to. Or, you can show a list of posts which contain specific key and/or value. The sky is the limit. We’ll write more about custom fields and how to use them in specific situations so stay tuned.

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

2 thoughts on “What are Custom Fields and how do they work

  1. Hey I know tis iss off topic but I was wondering if you knew of any widgets
    I could addd to my blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this forr quhite some time and was oping maybe you
    would have some experiewnce with something like this.
    Please let me know if you run into anything. I truly enjoy reading your blog and I look forward too your new updates.

Leave a Reply

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