Most WordPress users have seen the custom fields section when they write a new post or create a page - but what is it exactly?
With custom fields you can easily add extra content to your posts/pages, that you might not want shown directly in the post content, but as extra information along with the date, the title, the excerpt, the category, the author etc.
Examples
The WordPress codex mentions 4 great examples:
- Mood: Happy
- Currently Reading: Cindarella
- Listening To: Rock Around the Clock
- Weather: Hot and humid
Another way of using it could be something like this (I am sorry about the danish):
The top box saying “Socialsquare hjælper via kurser, workshops, foredrag …” will have different content on each page - this content is set in a custom field with a specific key.
How custom fields work
When you create or edit a page/post you can add a custom field. A custom field has two visible variables - the key and the value.
The key is the “ID” of the data - if you want to show your current mood for each post, you could call the key “mood”. The value is, the value. This could be “happy” or “mad”.
When you create a custom field for a post/page, the information you write will be stored with the post/page. In other words, the data you write will belong to the current post/page, and you will therefore be able to show the information along with your post/page.
How do I show the custom field data on the blog?
There is some different choices when you want to show the data with your post/page. I will mention two easy ways of doing it now and then return to some more advanced usages.
First of all you can choose to use the template tag “the_meta();”, this will automatically output an unordered list, looking like this:
The other way I will mention is get_post_meta($post_id, $key, $single = true). Using this you can list the custom fields just as you please. By using it like this get_post_meta($post->ID, “Key2″, $single = true);, it will return a the custom field value of “Key2″ as variable - this means you will have to echo it, like this: “echo get_post_meta($post->ID, “Key2″, $single = true);”
A little more advanced
I will take you through a couple of more advanced ways to use the custom fields now, but I won’t explain it into details - this will require a tiny bit of PHP knowledge.
So, you want to let a smiley show the mood you had while writing the post? Why don’t you start out by making the smilies - lets call them Sad.gif, Mad.gif and Happy.gif. Now, take a look at this snippet, and tell me what the value of mood should be, to show the Happy.gif smiley:
-
<img src=“/smilies/<?php echo get_post_meta($post->ID, “mood“, $single = true); ?>.gif” alt=“My mood” />
If you are reading books, maybe you want to show what book you were reading while writing a post - that is easy. But what if you were reading more than one book, and want to list them all?
WordPress allows you to use the same custom field key more than once per post. You can “group” these key’s by changing “$single = true” to “$single = false”. In other words, it will return an array. Take a look at this snippet:
-
<?php $books = get_post_meta($post->ID, “books”, $single = false); ?>
-
<p>I am currently reading:</p>
-
<ul>
-
<?php foreach( $books as $book ) {
-
} ?>
-
</ul>
Custom fields makes your life easier
Often when you look for a CMS, it can’t do what you want it to do out-of-the-box. By using custom fields you can quickly attach extra information, footnotes and the like to your pages and posts, and quickly change the way the information is shown, without having to edit 200 blog posts.
I hope the article can be of help to some people. I currently don’t use custom fields on this site myself, but I have made use of it on many client sites - it is just great!
87 comments
Building My Empire » Blog Archive » Into the world of Wordpress and those Custom Fields Says:
[…] If you’re blogging for money or if you use Wordpress to promote products or use it as your CMS, you might be like me and wonder about that option at the bottom of your writing of a post called “custom fields” - now I didn’t really know what they were for. No, I didn’t go surfing through the Wordpress Codex for the answer (which I could probably have found the answer… but well, I’m lazy). But I did find a great explaination for this interesting area through the Undersigned. […]
11/09-2006 | 21:03
ChrisLegend.net » Blog Archive » links for 2006-09-12 Says:
[…] WordPress how-to: Custom Fields With custom fields you can easily add extra content to your posts/pages, that you might not want shown directly in the post content, but as extra information along with the date, the title, the excerpt, the category, the author etc. (tags: wordpress howto) Save & Share This:These icons link to social bookmarking sites where readers can share and discover new web pages. […]
12/09-2006 | 2:59
karmatosed Says:
Great article and makes me start thinking of ways I can use these custom fields myself - they are an overlooked part of wordpress in my opinion and you just showed why they should be looked at more.
12/09-2006 | 12:59
The undersigned Says:
Thanks :) I don’t think the best way is to think about how you could use custom fields, but rather use it to solve a problem when it occurs - if you want something done with WP or if you want a client to be able to do something, but WordPress can’t do it out-of-the-box.
Someone has any great ways of using it? I know many are using custom fields for footnotes, related links to the posts etc. How do you use them?
12/09-2006 | 16:35
Nneka Says:
Just thought of this. Possible use for affiliate marketers: Serve up your own contextual ads.
12/09-2006 | 17:47
The undersigned Says:
Nneka, thats a good one :) Easy management of ads/sponsors for a specific article.
13/09-2006 | 7:23
Örvar Says:
That
that you have in the
calls is wrong. I would think that PHP would through an error since I don’t think assigning a value to a variable also returns the value. Use just
instead — and you can skip it in your last example since
is the default value for the third argument of
.
17/09-2006 | 1:25
The undersigned Says:
Örvar, yes, the variable could just be passed like ‘true’ or ‘false’, but this should work as well.. And despite the fact that the standard value of $single is false, I chose to keep writing the value in the guide to keep all the values visible to the reader, without being forced to explain about the default values etc.
17/09-2006 | 20:12
Örvar Says:
Yes, it works since what I thought was wrong and variable assignment in PHP also returns the value. But that $single in the arguments is not the same as the $single within get_post_meta function. The second only has local scope within the function. PHP passes arguments by value by default so what happens is that you have a wierd assignment in the function call that also returns the value, PHP then takes the value and creates a new temporary variable within the function itself.
17/09-2006 | 21:18
Jim Says:
Thanks for the great tips! I knew what these were for but never really had a use for them.
24/09-2006 | 16:33
Blogging Challenge: WordPress Treasure Hunt at MaxPower Says:
[…] custom fields — explained […]
06/10-2006 | 22:16
Travis Says:
I have been beating my head against the keyboard for the better part of 4 hours trying to get the post meta info to display correctly. For some reason, following your example(s) doesnt produce any result. Oddly enough, the_meta() does produce results.
I use the following for my “Asides” category to attempt to show the source of the info.
15/11-2006 | 20:41
Travis Says:
Whoops, all the code didnt come through.
Basically, the same way you did your advanced method 1.
15/11-2006 | 20:47
spldeytegy Says:
ggzes
23/01-2007 | 18:40
PeopleStrange Says:
Hi, thank your very much for this help :), i have a question, it’s possible put the content of the some custom fields in the feed?
13/02-2007 | 10:24
Koehly Says:
Great article Thanks :)
16/02-2007 | 15:22
Benje Says:
Thanks for the article, I spent a while having trouble with the get_post_meta function, but after reading this I realised that you needed ” marks around the key. Cheers!
18/02-2007 | 19:53
hip Says:
Great article! This was very informative and very useful, thanks a lot for taking the time to post this.
02/03-2007 | 13:05
Amit Patel Says:
Hi,
Is there are automatic plugin for this?
This also helps but seems to be a bit time confusing.
Amit
18/03-2007 | 6:00
John Says:
Thanks for all the explaination–been having some trouble -hopefully It would be much easier now.
21/03-2007 | 23:04
Allen Harp Says:
Thank you. This is the first time I’ve seen good instruction that I didn’t have to pay for in advance. You’ve answered a few questions I had already.
Thanks again.
23/03-2007 | 1:02
Rand Says:
I would have loved more with auto-plugin as i am not a good on anytning technical! Hope fully you can explain that too.
Thanks anyway–I’ll check back for any update:D
23/03-2007 | 3:12
Custom fields de WordPress » La mate por un yogur Says:
[…] WordPress how-to: Custom Fields […]
23/03-2007 | 11:17
Raul Vera (azul) » Blog Archive » Acerca de loa Campor personalizados (Custom fields) de WordPress Says:
[…] WordPress how-to: Custom Fields […]
25/03-2007 | 16:15
onemak Says:
thx very much for this,
i had a lot of problems with it //:
31/03-2007 | 22:19
Cruise Says:
Nice Buddy. You all replied many questions in my mind.
10/04-2007 | 10:06
Sunang Says:
Currently I find a bit difficult to work with WP specially when it comes to custom fields and plugins compared to v2evolution –but I am still using WP for my blogs dur to other great resources like themese and popularity.
It would be really nice if great contributors to WP community like you can develop some auto-plugins as Rand says. It would really be helpful for technically weak bloggers like me.
Regards
12/04-2007 | 15:46
Ally Says:
This really sounds and looks great. However for a noob or one who doesn’t know much about coding, is there some plugin which can easily get us going. I usually have lot of troubles customizing my blog.
Thanks
18/04-2007 | 22:12
Designer Ella Says:
This tutorial was invaluable to me.
I see you guys are interested in ways of using it:
http://www.colour-dreams.net/green-it/shop/market-totes/to-market/
I’m building a store and don’t have an actual product image even yet (well I don’t have the product!) LOL. So excuse any mess. There WILL be mess.
The color options are set with the multiple option code you gave, and the price is with the single. I also used the single custom field data in hidden fields in the shopping cart form.
I couldn’t build this site without you!
20/04-2007 | 16:15
delinetciler Says:
Great article Thanks :)
25/04-2007 | 16:41
Security Says:
it is an excellent idea!
27/04-2007 | 1:36
E Internet Index Says:
Great News Thanks for Sharing.
08/05-2007 | 12:59
ArjanV Says:
Hi,
I;ve using custom fields and it works but the output is sorted in alfabetical order. I don’t want that.
For example. my custom fields:
Type
Weight
Lenght
Height
Width
I want this order, but when i view my post it displays:
Height
Lenght
Type
Weight
Width
Anyone knows how to solve this?
10/05-2007 | 14:28
Et Voila! Blog Redesign Completed | graphic design art and typography blog Says:
[…] The WordPress documentation on Custom Fields is confusing at best, but once I stumbled upon this post, I was ready to roll. This is how it works <div class=”entry”> <p><?php $mythumb=get_post_meta($post->ID, chichaithumb, $single = true); if($mythumb!=”) echo ‘<img src=”‘ . $mythumb . ‘” class=”entrythumb” />’; ?> <?php the_content_rss(”, FALSE, ”, 68); ?></p> </div> […]
17/05-2007 | 9:09
johno Says:
Arjanv,
Can you show me the code you’re using to output this. Maybe I can help.
20/05-2007 | 15:40
ArjanV Says:
[To JOHNO]
“>
–>
20/05-2007 | 18:57
ArjanV Says:
this is what i’m using “”
20/05-2007 | 18:59
johno Says:
Arjanv
looks like your code is not displaying in your comment. You can go to my blog click on send us a message - then send me the code - I’ll take a look at it for you :)
20/05-2007 | 23:02
Web Hosting Review Says:
Thanks alot, very useful and informative.
21/05-2007 | 3:04
Avik Says:
Nicely explained…indeed. Though I still think custom field is a bit complicated stuff for non-techies.
26/05-2007 | 7:12
Ryan Says:
Great resource thanks for sharing.
26/05-2007 | 14:59
Luke Says:
Good examples and tutorial. Found it while searching for information on how to sort posts by the custom field assigned to them. Thank you.
03/06-2007 | 21:31
One Way To Use Custom Fields » WPDesigner Says:
[…] Recently, I learned how to manipulate custom fields from theundersigned.net. Today, I finally switched from excerpts to custom fields to display theme thumbnails on Wpdesigner. I’m talking about the three theme blocks in the header. Each block has an image, the theme’s name, theme author’s name and link, and the download page link. […]
05/06-2007 | 9:01
add url Says:
Great information thanks
11/06-2007 | 10:21
Steven Wong Says:
Good tip, this help me to clear my question long time ago.
Can I put the setting of not showing input from the colume?
Thank you for sharing.
14/06-2007 | 4:18
Ben Inside - i.am.Ben’s Blog » WordPress 自定义字段的一种用法 Says:
[…] 最近, 我从 theundersigned.net 那里学会了怎么使用 自定义字段(Custom Fields) . 今天, 我终于转用 自定义字段(Custom Fields) 而不用 摘要(excerpts) 来显示那些 Wpdesigner 的主题的缩略图了. 我说的是图片里面的三个缩略图, 分别包含各主题的: 名称, 作者链接, 以及下载链接. […]
14/06-2007 | 14:23
Wezp Directory Says:
Good examples and tutorial. Found it while searching for information on how to sort posts by the custom field assigned to them. Thank you.
14/06-2007 | 16:37
sivasnakliyat Says:
Thanks alot ;)
14/06-2007 | 23:03
komedi Says:
thanks wonderful document
14/06-2007 | 23:04
Wordpress Theme, Plugins, Tips, Seo » Blog Archive » One Way To Use Custom Fields Says:
[…] Recently, I learned how to manipulate custom fields from theundersigned.net. Today, I finally switched from excerpts to custom fields to display theme thumbnails on Wpdesigner. I’m talking about the three theme blocks in the header. Each block has an image, the theme’s name, theme author’s name and link, and the download page link. […]
21/06-2007 | 22:10
Custom Fields in WordPress » SilentBits | WordPress, Themes, Plugins, News, Blogging, Design, theme wordpress, plugins wordpress, blog wordpress, template wordpress, tip wordpress, tag wordpress, free theme wordpress, tutorial wordpress, blog theme Says:
[…] You can read the full text here: WordPress how-to: Custom Fields and think about how to use with your website. […]
22/06-2007 | 22:12
Zuqy net Says:
Thanks a lot usefull resource
26/06-2007 | 17:18
Zuqy net Says:
Thanks a lot usefull resource
26/06-2007 | 17:18
AddA Says:
Thanks a lot with this excellent resource.
03/07-2007 | 13:34
Katie Says:
Thanks for posting this, it was very useful and easy to follow
10/07-2007 | 11:24
Thibaud ALEZIARDO Says:
totally INSANE :-D
13/07-2007 | 15:47
sohbet Says:
Sohbet
16/07-2007 | 8:54
bssn Says:
it’s a very great example of using custom field.
I think I can master it now, thank U for sharing this experience!
it helped me so much!!!
08/08-2007 | 7:00
Flywheel Canada Says:
Interesting and very informative. I have to study more to perfect it.:)
09/08-2007 | 7:55
ariel Says:
great article and can you give me assist in displaying list of post which has a specific custom field value?
10/08-2007 | 10:56
chris Says:
great tutorial on adding smileys to wordpress
19/08-2007 | 19:46
proxy Says:
thank you for your excellent course!!! I got so much!!!
20/08-2007 | 3:12
Kevin Says:
Thanks for the post, it pointed me in the right direction regarding custom fields.
29/08-2007 | 16:10
Copes Flavio Says:
Thanks for this! i’m in the process of translating my blog, keeping both the italian language and both the english one.
I’m not going to use the available plugins since they don’t work as i want, so i will use custom fields to have a link between the translated articles between my english blog and my italian blog. Reading this article I understood how these work! :-)
30/08-2007 | 17:32
WordPress 自定义字段 | 我爱水煮鱼 Says:
[…] 翻译自:WordPress how-to: Custom Fields 约有删节,请注意。 […]
04/09-2007 | 15:16
Śmieszne filmy Says:
totally INSANE :-D
08/09-2007 | 20:58
Dathan Says:
Some great tips here, thanks undersigned!
29/09-2007 | 4:26
milo Says:
Better expression than on the official WP site…
09/10-2007 | 6:40
Lingerie Girl Says:
Do you realise that this page doesn’t work properly in internet explorer 7? The main content does not start until the navigation stops. Considering you are an expert, I would have thought you’d want to fix it. (I mean no offence - just incase you did not know)
11/10-2007 | 17:05
MCportal.pl Katalog stron Says:
Good article.
17/10-2007 | 23:49
aLUKEonLIFE Says:
THANK YOU! Having got no-where with WP help the info above was just what I needed.
18/10-2007 | 0:06
Manken Says:
Thanks for posting this, it was very useful and easy to follow
20/10-2007 | 11:41
Using WordPress Custom Fields: Introduction: Using WordPress Custom Fields tutorial series Says:
[…] The good starting point is checking out WordPress’ Using Custom Fields. It’s a very basic tutorial, but will give you some idea of what custom fields are. The Undersigned has a good tutorial also. Neither of these tutorials cover a lot of uses, and I think that’s why many people don’t know what to do with custom fields — there are not enough detailed tutorials. […]
24/10-2007 | 22:03
uttoransen Says:
thanks for the nice post, i was searching for this for some time, did not got the answer on any of the help forums! finally i got it here, thanks,
25/10-2007 | 15:28
Castelar Says:
“great article and can you give me assist in displaying list of post which has a specific custom field value?” bump!
31/10-2007 | 10:22
NGPOD国家地理 Says:
如何使用自定义字段的演示!
the demo of how to use Csutom Fields!!
haha
07/11-2007 | 2:33
Blogs Says:
华语/華語华语/華語
08/11-2007 | 21:20
Tipografo Says:
thanks, the custom fields array snippet is exactly what i was looking for
16/11-2007 | 16:07
The Top 5 Uses for Wordpress Custom Fields on the Oak Innovations Blog Says:
[…] page and the the_meta function. There’s also a nice run through of an implementation here and here. Popularity: unranked […]
26/11-2007 | 14:24
Cómo añadir más información a nuestros posts en WordPress » blogpocket 7.0 Says:
[…] Un ejemplo de uso avanzado (sencillo) […]
15/02-2008 | 0:08
Recently Read and Recently Heard Widgets Says:
[…] Justin has a great tutorial on getting started with custom fields. Thomas Silkjær also has a brief tutorial on his site that you might find helpful. Tim Trott offers up several good ideas in this article […]
21/02-2008 | 0:33
Test Blog » Blog Archive » What are custom fields? Says:
[…] a very basic tutorial, but will give you some idea of what custom fields are. The Undersigned has a good tutorial also. Neither of these tutorials cover a lot of uses, and I think that’s why many people don’t […]
28/02-2008 | 5:10
John Says:
Just what I needed to figure this out… thanks!
26/03-2008 | 15:08
Derek Perkins Says:
How do I duplicate custom fields across pages and posts? I have about 30 different fields that I want to add (product page information) to lots of pages, but I’d rather not manually enter in all the keys at the same time. Is there a good way to duplicate custom keys across posts and then just change the values?
09/04-2008 | 5:40
djoaniel Says:
its probably the simplest explenation how to display that string. thanks.
29/04-2008 | 3:50
WordPress 自定义字段 - DreamRainy Says:
[…] 翻译自:WordPress how-to: Custom Fields 约有删节,请注意。 […]
30/04-2008 | 12:06
Hans Hilmi Says:
It’s Basic of wordpress, But i like It…
Thanks…
04/05-2008 | 13:25
Leave a Reply