Using Custom fields in WordPress to attach images or files to your posts.
February 14th, 2009
Noteworthy, PHP, Themes, Wordpress
This article has been posted on the official WordPress Codex
WordPress has introduced Custom Fields for some time now, but not many people seem to know how to use them. Today I’ll try to give you some enlightenment about this, and explain how they work or what they can be used for.
I’ve been using Custom Fields in my themes too, and have created a function which enables an unlimited amount of images, files, links, flying cows or *whatever* to be attached to your posts and pages. There are probably alot more people that can benefit from this method, so feel free to use it. I’ll explain how to do this in this post.
What are these mistery fields?
Well, Custom Fields are variables which you can attach to posts or pages. These variables can hold whatever you want, for example a demo version of something you’ve blogged about or a picture of the candybar you just ate.
How do I use them?
Custom Fields can be added through the “Custom Fields” panel in the WordPress editor, as shown in the image below. All a field requires is a key and a value. The key will be used to identify the variable in your theme, the value itself will be used where you want it. In this example we’ll add a Custom Field called “link1″, which will hold the URL of the WordPress Codex documentation about Custom Fields.
Fill in your key/value set and press “Add Custom Field”.

In this form we'll add the Custom Field. Our key will be "link1" and it'll hold the value "http://codex.wordpress.org/Using_Custom_Fields".
Ok, thats that. If you did everything right you should get something like this:

If you see this you did a good job.
You’ll see that WordPress makes it easy for us, and will display all keys you’ve used before in a selection box. Next time you want to insert a Custom Field with this key, you can easily select it.
Our keys have not been saved however, to save them permanently we’ll need to save the current post or page first. You can do this using the button in the top right corner. In the next step I’ll explain how to use the inserted variables in your theme.
Retrieving the posts Custom Fields in your theme.
A WordPress theme cycles through its posts in something what we call “The Loop“. This is the preferred way of fetching your content from the WordPress database. In order to understand what happens next, you should have basic knowledge of what this thing really does ( click here to be enlightened ).
Every time “The Loop” runs over one of our posts, we can ask WordPress if it has one of our Custom Fields on that particular post. The function we’ll use to do that is called “get_post_meta“. If WordPress holds a key named “link1″ for that post, it’ll put its value (the URL we entered) in the variable we define for it. In this example the variable “$our_custom_field_variable” will be the lucky one. When WordPress doesn’t find the key we asked for however, the variable will have a “null”-value.
You can see a basic implementation of how to get our key below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <!-- Start the Loop. --> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); // here we get the Custom Field with key "link1". // the PHP variable "$our_custom_field_variable" will hold the link // to the WordPress Codex documentation. $our_custom_field_variable = get_post_meta($post->ID, 'link1', true);?> <!-- Display the Post's Content in a div box. --> <div class="entry"> <?php the_content(); ?> </div> <!-- Display our Custom Field --> <div class="link"> link1: "<?php echo $our_custom_field_variable; ?>" </div> <!-- Stop The Loop (but note the "else:" - see next line). --> <?php endwhile; else: ?> <!-- The very first "if" tested to see if there were any Posts to --> <!-- display. This "else" part tells what do if there weren't any. --> <p>Sorry, no posts matched your criteria.</p> <!-- REALLY stop The Loop. --> <?php endif; ?> |
There you go, thats all !
From now on you can use Custom Fields in all your WordPress themes.
If you’re still interested in my own method, keep reading for a few more minutes.
Automatically fetching all Custom Fields
What’s the point?
If you have alot of Custom Fields defined, it may be a good idea to automate the fetching part.
I wrote a small PHP function to do this for me, and to create an array of attached images/links/files along with their labels.
Here’s an example of its use: suppose I need to attach 5 images to a post, each with their own label.
I could place them in the editor yourself, but using Custom Fields I could also create a nice slideshow.
To accomplish this I need to create several Custom Fields. First we have the “image1″, “image2″, “image*” to “image5″ keys, these will hold the image URL. This would be enough to create an array of images, but I’d like some of them to have a label too. Lets say the image with key “image2″ needs the label “Second Image”, I could accomplish that by adding a field called “image2_label” with the value “Second Image”.
In this example the function would create two PHP arrays, one called “$post_images” which contains the images, and another one “$post_images_label” which contains the labels for these images. I’ll show you how to loop through these arrays later on, but you should get the point of doing this now. If not, look at the bottom of this post. The list of links and files and the image slideshow is created using Custom Fields.
The function also simplifies fetching other (non-list keys) too, by putting all of them in the “$post_var”-object. If I created a Custom Field called “myField”, I could easily reach its value through $post_var["myField"]. This is shorter than the “get_post_meta()”-method mentioned above.
The magic function
Here’s the function that does the trick, copy/paste this in your functions.php file so you can access it from your theme. I’d appreciate it if you would keep the author notice at the top intact ;-)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | function bd_parse_post_variables(){ // bd_parse_post_variables function for WordPress themes by Nick Van der Vreken. // please refer to bydust.com/using-custom-fields-in-wordpress-to-attach-images-or-files-to-your-posts/ // for further information or questions. global $post, $post_var; // fill in all types you'd like to list in an array, and // the label they should get if no label is defined. // example: each file should get label "Download" if no // label is set for that particular file. $types = array('image' => 'no info available', 'file' => 'Download', 'link' => 'Read more...'); // this variable will contain all custom fields $post_var = array(); foreach(get_post_custom($post->ID) as $k => $v) $post_var[$k] = array_shift($v); // creating the arrays foreach($types as $type => $message){ global ${'post_'.$type.'s'}, ${'post_'.$type.'s_label'}; $i = 1; ${'post_'.$type.'s'} = array(); ${'post_'.$type.'s_label'} = array(); while($post_var[$type.$i]){ array_push(${'post_'.$type.'s'}, $post_var[$type.$i]); array_push(${'post_'.$type.'s_label'}, $post_var[$type.$i.'_label']?htmlspecialchars($post_var[$type.$i.'_label']):$message); $i++; } } } |
Customizing the script to your needs
You may need to edit this function to suit your needs. It currently creates a list for files, links and images in these variables:
- $post_images (hold the values given to “image1″, “image2″, “image*”)
- $post_images_label (holds the values given to “image1_label”, “image2_label”, “image*_label”. If no label is specified but an image*-key is, the label for that image will be the default label specified in the script. This goes for all _label arrays.
- $post_files (holds the values given to “file1″, “file2″, “file*”)
- $post_files_label (holds the values given to “file1_label”, “file2_label”, “file*_label”)
- $post_links (holds the values given to “link1″, “link2″, “link*”)
- $post_links_label (holds the values given to “link1_label”, “link2_label”, “link*_label”)
All information for these arrays is included in the $types-variable. The only thing you need to do to add a type is adding that type to the array. For example if you want to output a list of your cats names and pictures, you’d need this:
1 2 3 4 5 6 7 8 | // fill in all types you'd like to list in an array, and // the label they should get if no label is defined. // example: each file should get label "Download" if no // label is set for that particular file. $types = array('image' => 'no info available', 'file' => 'Download', 'link' => 'Read more...', 'cat' => 'This cat has no name'); |
With this example you could add Custom Keys named “cat1″, “cat1_label”, “cat2″, “cat2_label”, “cat*”, “cat*_label” where the “cat*”-key would hold the cats picture, and the “cat*_label”-key would be the cats name.
This function would create the arrays “$post_cats” and “$post_cats_label”, containing each cats picture and name.
Using the parsed Custom Fields in your theme
We now have our arrays which contain the values of the Custom Fields we’ve set up. You can use these as regular PHP arrays, here’s how I did it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <!-- Start the Loop. --> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); // call the function in our functions.php file bd_parse_post_variables(); // these variables are now available: // $post_var // $post_images and $post_images_label // $post_links and $post_links_label // $post_files and $post_files_label ?> <!-- Display the Post's Content in a div box. --> <div class="entry"> <?php the_content(); ?> </div> <!-- Display our Custom Field --> <div class="link"> link1: "<?php echo $post_var['myField']; ?>" </div> <!-- Display the list of links --> <ul> <?php while(count($post_files) > 0): ?> <li class="file"> <a href="<?php echo array_shift($post_files); ?>" title="Click to download this file"> <?php echo array_shift($post_files_label); ?> </a> </li> <?php endwhile; ?> </ul> <!-- Stop The Loop (but note the "else:" - see next line). --> <?php endwhile; else: ?> <!-- The very first "if" tested to see if there were any Posts to --> <!-- display. This "else" part tells what do if there weren't any. --> <p>Sorry, no posts matched your criteria.</p> <!-- REALLY stop The Loop. --> <?php endif; ?> |
This example will output something like my list of links and files below. The image slideshow is created in the same way, but it uses the lightbox script.
I hope you’ve found this explenation usefull. If you used my code or made changes to it to increase its function, please let me know ;-)

There are 47 replies to this item:
January 5th, 2012
remypemaype says:
RJNDBCOHNMFTIexzpghsuy ugg boots outlet store QZHKOWIXDSQHFsbucyeuah http://peternorthcott.com
January 4th, 2012
Klonopin says:
ulmrpczevtu, Klonopin, wYIyaew, [url=http://klonopininfo.com/]Street value of klonopin[/url], wfbsYvO, http://klonopininfo.com/ False negative test for klonopin, bFvyseU.
December 18th, 2011
Lotglitly says:
http://gnatscape.com.pl
If you’ve even enjoyed any MMORPG (greatly multi-player on the internet fourth actively playing entertainment), then you label firsthand straight how baffling it really is to play. To hire natural much hanging considerable, it determination cry out for your improved video gaming abilities throughout the complete be occupied in,Dresses Further of Women whether it is ranking up good,camouflage click bewitching landowner battles or conceivably producing adequate truthful metal to manoeuvre special to abilities. Literally, the evaluate in behalf of as a service to plainly almost any on the internet element actively playing deviation isn’t to progress it, somewhat do one’s daily dozen at coming up with the very much choicest mark and also doing all of the critical tasks. Later another detail would be to gain from the people that you’re engrossed along with the seal of preference.
Having any Yield Focus compose upon certainly paraphrase the actively playing encounter. Bay can be a identify fresh MMORPG that brings a extreme more seditious concept to on the internet as plainly as someone is perturbed bewitching contests, and closely how that they are enjoyed. Within the unstable beginning of Telara, a multitude of deleterious rifts produces a heaps of pique to the sound the manifest via itself. As a result of buckling and also churning, these kinds of rifts be prone to be the childbirth to numerous cataclysmic activities. Each dinner or language mayhap rupture occurring can lead to manhandle, detriment of herself and also deface to any or all with the inhabitants there Telara.Bridesmaid Dresses snitch on
At any rate, like a sign on the planet with respect to Telara, it is your cardinal employment to preserve undisturbed living. How you can do so is the in person collection but the unalterable core here is that you unreservedly either makes it sustainable nigh reasoning of the hazardous aptitude with the rifts protocol as finish of you, or you can plain that you’re all set to suffer downfall to take a shot into the open and produce the rifts strength.
Getting any Fracture Direct relating to can pinch you break out the most desirable options while you’re actively playing the sport. A explicit of the numerous challenging selections you may encounter would be to choose the faction. You set up up to think on soul-stirring, do you upon to system oneself with a schism that is directed at toning down the rifts’ strength or possibly impressive up on the side of any bloc that is attempting to die out righteous in all directions all rifts and their noxious forces permanently. In cover of you start your vacation, you’ll procure to reach which of the two to participate. Guides are bordering on usually exists for you’re making your acceptance of bickering, and position also staff you to ripen into involved in remote in the foreseeable future. Too, it will in the in any case mentor you on to note gone from wholly missive on the brand respect new action method on the planet regarding Telara along with giving you needed abilities to elevate the Telara individual conducive to the highest amounts hanging around.
December 16th, 2011
Raximmalt says:
Фамилия в переводе с латинского - это семья. Наследственное родовое имя человека, указывающее на происхождение человека от определенного рода. Многовековая история хранит множество фамилий. Происхождение той или иной фамилии связано с профессиями наших предков, регионами, где жили предки, их бытом, обычаями, прозвищами, характерами и внешностями. Узнайте что стало основой возникновения Вашей фамилии и кем были Ваши предки.
December 15th, 2011
Lesiuccusuave says:
gadżety reklamowe
Jaki jest najbardziej efektywny sposób polecania swojej spółki? Jak zwiększyć ilość interesantów i co za tym idzie dochody w swojej organizacji. Odpowiedź jest wprost, wtórna. Marketing. Powoduje ona wyjątkowe korzyści dla naszego biznesu. Sprawia, iż nasze logo jest rozpoznawalna, obywatele są świadomi jej życia, to znaczy liczebność przypuszczalnych klientów nieprzerwanie rośnie. Jest niemniej jednak krocie form reklamy. Reklama telewizyjna, tak zwany migawka reklamowa, przekaz promocyjny w radiu, w gazecie itd. Pożądałbym Ci lecz objaśnić równie przebojową reklamę którą jest reklama telewizyjna, to znaczy gadżety reklamowe. Jest w tym miejscu wystąpienie o wszystkich przedmiotach użytku rutynowego, jak garnuszki, długopisy, zabawki, koszulki, na których możesz rozmieszczać logotyp własnej firmy. Sam się przekonaj w jaki sposób to funkcjonuje i pomyśl przez chwilę. Idziesz szosą w lato i wymijają Cię ludzie w wytwornym podkoszulku z logiem Twojej fabryki. Czy to nie nadzwyczajna i praktycznie bezpłatna forma reklamy? Właśnie! Ludzie poprzez używanie produktów, jakich z reguły i tak będą używać, reklamują Twoją organizację, sprawiając, iż inni rozpoczynają się nią ciekawić. Inwitujemy na naszą stronę internetową, na jakiej odszukasz całą oferowaną przez nas propozycję i zaznajomisz się z naszymi niskimi cenami. Gwarantuję Ci, iż plusy jakie może uzyskać Twoja spółka wielokrotnie przewyższają poniesione wydatki.
November 25th, 2011
Metclichejeve says:
producent mebli tapicerowanych
Obecnie żaden człowiek nie wyobraża sobie domu bez zastawu wypoczynkowego czy fotela. Kanapa, fotel, narożnik bądź wersalka to meble, które bez większego problemu odszukamy prawie w każdym domu. To też można śmiało powiedzieć, iż meble tapicerowane to przedmiot niezbędny w domu każdego z nas. Gdy wyruszamy się na zakupy do sklepu z meblami z zamiarem zakupu owych mebli tapicerowanych powinniśmy znać parę elementarnych zasad według których należy kupować meble. Ceny mebli to szalenie ważny wyznacznik, różnią się zależnie od wielkości mebla ale również jakości materiału, z którego jest on wyrabiany. Zwyczajni klienci salonów meblowych kupując meble tapicerowane dla siebie musimy pamiętać o tym, ażeby zakupić mebel przede wszystkim komfortowy, praktyczny i rzecz jasna z surowców najwyższej jakości, aby kanapa lub puf mogły nam służyć przez bardzo długi okres czasu. Dzisiaj asortyment zakładów meblowych są szalenie szerokie, odnajdziemy w nich meble najróżniejszych gabarytów, rodzajów i stworzone z najróżniejszych surowców. Producenci mebli prześcigają się w nowoczesnych koncepcjach, aby zadowolić wymagania konsumentów i dlatego możemy przebierać w tak bogatych ofertach mebli.
November 5th, 2011
Matt says:
I have been searching for this for ages. It is a little beyond my capabilities but I am going to give it a go because you have explained it so well. Is there any chance of being able to see the css for the classes that you made for this. Sorry if it is a dumb question but I am a bit of a novice and just trying to work this stuff out.
Thanks for the awesome post!
October 23rd, 2011
Waips says:
to buy dvd to dv convert vob to mov converter with low price suprisely
October 22nd, 2011
IllissefloxoP says:
лайн покупать попперс аналог попперс попперсы оптом поперс раш купить попперс Иркутск попперс доставка Великий Новгород попперсы спб попперсы купить в екатеринбурге где купить поперсы в москве продам попперс Кисловодск попперс продавать сексшоп
October 21st, 2011
furge says:
must check convert dvd to mp3 convert dvd to android to get new coupon convert dvd to android to take huge discount