
I can’t believe I’ve been playing with Wordpress for so long and never explored the Custom Fields feature. A website I’ve been working recently has required me to push the boundaries of Wordpress, or more appropriately my understanding and knowledge of it. In doing so I’ve bravely slipped deeper into the Advanced Options and lived to tell the tale - the tale of the Custom Fields…
The fundamental functionality of Custom Fields provides you with the ability to expand your posts and their content. They allow you to include extra information and features in your posts that the WYSIWYG editor doesn’t provide itself.
How it works.
When in your Write/Manage posts page, mooch down past Tags and Categories to the Advanced Options - ooh! Down here in these murky depths you’ll encounter the Custom Fields box. In here you’ll see 2 empty textareas labelled key and value.
The key is the name for your custom variable, and the value is, well, it’s value! When you create a custom field, you are creating new meta-data. You then insert a simple line of php into your Wordpress template where you want this extra data to appear in the theme and voila! You’re done.

So, the easiest way to show you is with a short example.
Adding Subtitles to your post
I feel subtitles serve more as a presentational feature than anything. And sure, you could always just insert a h2/h3 at the top of your post to act as a subtitle. But sometimes that doesn’t cut it, especially if your subtitle needs to stand out from the post content more than an standard inline header.
Step 1 - Theme files:
In your Wordpress theme, you need to enter a line of code to pull in your custom field. Make sure this is within the post loop, the most obvious place is of course directly under your post title. Here you go:
1<?php $values = get_post_custom_values("Subtitle"); echo $values[0]; ?>The only bit you may wish to change is the keyword within the parentheses. In this case ‘Subtitle‘. This ties in with the key you create in the post editor, so make sure they’re the same. And only use it again if you wish to display the subtitle somewhere else, for any other custom fields you’ll have to change this.
It would make sense to place this code within header tags:
1<h3 class="subtitle"><?php $values = get_post_custom_values("Subtitle"); echo $values[0]; ?></h3>That’s it for the code!
Step 2 - Post page:
When you’re writing or editing a post, go down to the Custom Fields area in the Advanced Options. Create a key which correspondes to your keyword you set in the code. The value will be the actual Subtitle content. So for example: Key - Subtitle Value - An inside look at Google’s new browser… Add your custom field and save your post.
Step 3 - Check it out:
Check out how it displays on the page. Then utilise the power of CSS to get it looking bob on!
Having more fun
Let’s face it, subtitles aren’t that interesting. But there’s a lot more you can do, a lot of which I’ve still yet to discover. Post Thumbnails
Add the following code to your theme:
1 <img src="/wp-content/post-images/<?php $values = get_post_custom_values"Image-Thumb"); echo $values[0]; ?>" alt="" />This will include an image, pulling it in from a specific folder. So if you upload all your thumbnail images to the directory /wp-content/post-images/ and then in your post editor set the key as ‘Image-Thumb’ and the value as your image name (eg. picture.png), this will pull in your thumbnail picture and place it in your post. You can control the appearance of the image with CSS.
To actually upload your images to the specified folder, you can use good old FTP or a plugin I’m particularly fond of, because it keeps you in the WP admin area without having to use any external programs: Filosofo Old-Style Upload.
Of course this doesn’t necessarily have to be a thumbnail, it can be a full size image, adding plenty of colour and interest to your blog articles.
For a more advanced alternative to this method, check out Justin Tadlock’s method.
Meta-Data
Some of the examples given on the Wordpress Codex page on Custom Fields are the simplest, and the best. What if you fancy adding information relating to your current mood or the weather at the time of writing your post? Try:
Current Mood:
Key: Mood
Value: Happy
Code:
1<span>Current Mood: <?php $values = get_post_custom_values("Mood"); echo $values[0]; ?></span>Displays: Current Mood: Happy
Listening To:
Key: Listening-to
Value: Fly me to the moon - Frank Sinatra
Code:
1<span>Listening to: <?php $values = get_post_custom_values("Listening-to"); echo $values[0]; ?></span>Displays: Listening to: Fly me to the moon - Frank Sinatra
Other ways to display meta-data
To pull in all the meta-data for a post in one fell swoop, go for:
1 | <?php the_meta(); ?> |
Which will display all the meta-data in an unordered list and add’s classes that can be styled with CSS.
If you want to pull meta-data from a specific post, you can specify which post by the post ID, which data by it’s key and whether to display the data as a single item or pull in an array:
1 | <?php get_post_meta($post_id, $key, $single); ?> |
- $post_id is the ID of the post you want to select data from
- $key is the name of the meta-value you want
- $single can be either true of false. If true it will pull in a single string of meta-data, if false it will pull in an array of the custom fields.
Go Play…
Like most things, Custom Fields open doors which you have to walk through and explore. In essence they’re very simple but if used correctly can be pretty powerful, another extension of the basic functionality of Wordress. So go forth and experiment!
For more information on Custom Fields and all things Wordpress - visit the WP Codex.





















Brandon Cox
I love this post! I am just now beginning to explore the custom fields feature for thumbs, and am glad to see a tutorial on doing even more. Thanks!
Jin
Good post.
I use custom fields extensively on my blog entries, since they all have different background images and title color. That’s another reason I love WP.
I’d like to point out, I think a null value check should be in place, before outputting the value.
For example, the following code determines what bgcolor to use for a particular blog entry. if no custom field is assigned, it uses a default value. (i’m not using brackets, in case it doesn’t go through the comment)
$bgcolor = get_post_meta($post->ID, ‘bgcolor’, true);
$bgcolor = $bgcolor ? $bgcolor : ‘#888888′;
/– inline header css –/
background-color: ?php echo $bgcolor ?;
Noura Yehia
That’s a nice cheat-sheet.. Bookmarked for future reference
Clay McIlrath
Cool, i never really thought about what those custom fields could be used for until now.. but i could create a whole set of classes to use for specific posts.. that’s pretty sweet. Do you know if you can make function calls with those fields as well?
nathan
Thanks for the comments, I’m glad this tut is of use.
@Clay - this article strained my programming knowledge as it is. I’m not sure what you mean by function calls?!
James
Nice post nathan, As to clays post i doubt that you can use function calls within these, Just so that every one is clear a function call is where you call one of the statements within the system an example is get_post_time(), I may be wrong just dont have the time to test it, By the way clay what would you want to achieve via this anyway.
ahnShev
very usefull. thanx.
adkdev
Thanks a lot…
Mandy Bee
I’ve always wondered what I might be able to use those custom fields for but I never took the time to look into it. Great post! Thanks!
Design Bliss
Thanks! I’ve been wanting to learn more about custom fields. This explains a lot.
Clay McIlrath
@james.. well i’ve been working on my own system that builds on top of wordpress.. it’s for a client that gets high traffic and rather than a generic contact form, they want a few different contact forms that would relate to different posts. For example, the site is a sports networking site.. They have 5 writers for up to 10 different sports that all post in the same blog. Sometimes they want to rate feedback or do a poll but only for a specific post.. SO i was looking for a way to call different functions to pull in those post-specific forms.
If this plugin doesn’t do that know of any that would..? I figured i’d probably have to write it myself because i doubt many have a use for that
Nathan Cook
I don’t *think* you can use it to call functions. But! You could define a special key that when it has a value would trigger a switch statement that then reads the value of your custom field and runs a specific function based on the value. I am responding on my mobile, and not ambitious enough to put together a code sample. if you have questions e-mail me and I will make another post here. nathan@devstu.com
Clay McIlrath
Mmhmmm, that’s not a bad idea, I may do that indeed. No need for code examples, I know what you mean. Thanks
Chad
You may enjoy the Custom Write Panel plugin.
Ever since I discovered the power of Custom Fields, this plugin as well as the RC Custom Fields plugin have been awesome.
FAWiKi
Hi! The post is really interesting! I’ve read your blog and can say it’s a good job.
inveleava
Great blog. I like layout!!!!
Jason King
So thts wot those custom fields were for?! Nevr realised their potential til now, Ill c wot I can do with them in future..
Top site Nathan, tnx!!
Johan Dole
Thanks for a pedagocial article.
Have you heard about Flutter? Seems to be an incredble Custom Fields-plugin.
Celosia
Thank you! The theme that I’m currently using has a custom folder containing a custom CSS file and a Custom functions file (custom_function.php). Just want to know if it’s possible to just modify the custom_function.php instead of modifying the main theme files.
nathan
Thanks again for the great comments and advice.
I’m afraid this is the extent I’ve really played with Custom Fields so far, I’ve not tried any plugins and @Celosia I don’t really know anything about custom_function.php! If I had time I’d look into it all for you but I’m afraid I’m just really busy at the moment.
Thanks guys, I’ll get another decent article out soon.
Nathan
liz
very cool! I’ll have to try this out!
Aaron Irizarry
Great article very useful thanks!!!
createmo
Thank you for your site
I made with photoshop backgrounds for youtube, myspace and ect..
my backgrounds:http://tinyurl.com/6ptkxd
all the best and thank you again!