WordPress: How To Display Multiple Values of a Custom Field Key
WordPress | March 25th, 2009In one of my recent adventures with WordPress I came across the need of having one or more custom fields values listed in one page. In my case, I wanted to list press quotes from theatre performances.
This is quite simple and useful, so I decided to write a quick post that may be useful to someone else.
Assumptions
I’m assuming you’re familiar with WordPress, so I’ll just skip the part where I’m supposed to explain how great custom fields are and how they add so much value and power to WP. It’s all true, but this is a “quick post”.
My requirements
As I mentioned, I needed to list quotes from the press for a specific theatre perfomance. Some performances had no quotes at all, while others had several. I also didn’t want to see the “Press Quotes” title, unless there were quotes. So my short list of requirements was something like this:
- Check if there are any custom fields in the post.
- If not, show nothing.
- If there are, echo the list of values, but not inside
<li>tags (which is the default behavior if you useget_post_meta()). I want my values to be listed inside a<blockquote>and a<p>, to be semantically correct.
The code
I’ll assume we’re working within The Loop here, so wherever you’re adding this code to, it should be contained within something that may look like this:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?> <!-- This is just a comment, your code should come here --> <?php endif; ?>
Inside the Loop, add this bit of code:
<?php
$press_quotes = get_post_meta($post->ID, "pressquote", false);
if ($press_quotes[0]=="") { ?>
<!-- If there are no custom fields, show nothing -->
<?php } else { ?>
<div class="pressQuotes">
<h3>From the Press</h3>
<?php foreach($press_quotes as $press_quotes) {
echo '<blockquote><p>'.$press_quotes.'</p></blockquote>';
} ?>
</div>
<?php } ?>
This is all you need to know:
$press_quotes: This is the variable that will store the value of each custom field. Replace by whatever suits you best (that’ll be 5 times in this code).$press_quotes = get_post_meta($post->ID, "pressquote", false):pressquoteis the actual Key of my custom field, as you can see in the image bellow:

If there are no custom fields, the code will echo a handful of nothing (or my little comment).
If there are, I’m showing the h3 tag with my pretty tittle, everything within a convenient div, for placement.
Then, I have to say what I want to repeat for each value (for each quote, in my case). So I write exactly that after the “echo” bit:
'<blockquote><p>'.$press_quotes.'</p></blockquote>'
Make sure to add the repeated HTML code within ‘ ‘ and the variable within . .
It’s as simple as that!
It may not be the only (nor the better) way of doing it, but it’s easy enough and it works. Let me know if you have any other methods!
References
- WordPress Custom Fields, Part II: Tips and Tricks
- WordPress Codex: Using Custom Fields
- WordPress Codex: Function Reference/get post meta
- WordPress Codex: Function Reference/get post custom
- WordPress Codex: Function Reference/get post custom keys
- WordPress Codex: Function Reference/get post custom values





Wordpress seems to be an exciting resource for fast website development.
I didn’t had the time to dive into the doc’s but from what I can see from my own blog, I’m sure that I will have lots of things to mess around with it.
For me it’s to soon to state that WordPress is or not suitable for more complex site requirements, but for sure, a simple a slick backoffice operated website, WordPress will surpass other CMS’s on the market, just as long is kept simple for the developers.
BTW: Olá Lili :D
Ey your blog is very good, the design and the information. i will back here for more.
About this post i am experimenting too with custom fields so ill give it a try
Hello.
I would highly recommend plugins like Custom Field Template or (even better) More Fields. These help make the custom fields feel a lot more part of the admin interface and less techy. They also come with automatic field types. This is great for client sites.
Keep up the good work!
Great post, but one correction- this code doesn’t necessarily need to be used within the loop.
The only time this *needs* to be within the loop is if the custom field values are being pulled from a post, and then only because this code needs $post->ID to be defined.
If you’re using this code to display custom field values of static pages, or if you’re getting the post ID through some method other than “$post->ID”, you can use this wherever you like.
thank you so much! really great trick!
yeah! :D
I tried using this and I get an error message with foreach:
Warning: Invalid argument supplied for foreach()
this is the code i’m using
ID, ‘affiliates’, false); ?>
<?php foreach($affiliatesfields as $ads) {
echo '’.$ads.”;
} ?>
Hi, I am looking for a way to display many images based on custom fields.
I am working on a type of Image gallery where a post will have multiple images and I just don’t wanna have to insert them withing the post directly.
Any help will be appreciated.
By the way, I am modifying the theme “No-frills 2.1.7 by Jess Kim”
@Kathy: try using
foreach($affiliatesfields as $affiliatesfields)
instead of what you’ve got.
Anyone know of a way to then sort the custom fields in order to display them in a controlled order? They seem to display at random, how are they currently sorted using this method?
No idea. Wondering the same thing!!
Figured it out and wrote a post about it here: http://www.eliasinteractive.com/blog/sorting-data-from-custom-fields-in-wordpress
@Luke: Thanks for the comment. Sorry I couldn’t be of any help though :(
I’m often looking for brandnew articles in the WWW about this theme. Thx.
This was perfect! It was exactly what I needed to get thing rolling. Of course I had to change it because I was using it to pull images for an auto scrolling feature on a client site .. BUT .. it pointed me in the right direction. THANK you!
Very good explanation! It was definitely something I was looking for to display a rotating testimonial plugin for a client’s sidebar.
Awesome, I didn’t heard about this topic till now. Thx!!
Действительно вы очень интересно пишите, не зря мне друзья посоветовали, спасибо за посты!
Thanks so much for this! Works perfectly for the similar pull-quote effect I needed.
Lovely blog and work, as well. I’ll certainly be subscribing. Cheers.
I AM VERY HAPPY BECAUSE I GOT HELP FROM THIS FOR GIVING CUSTOM VALUES
I used this for entering Paypal “Pay Now” buttons! Works awesome and is way easier than entering it into the “HTML” or “Visual” mode in the WordPress backend.
THANKS!
I have been going slightly crazy trying to deal with the issue. I have tried a few different idea’s, but this one is a winner for my needs.
I am using it in almost the exact way described, so i don’t need too much modification.
Thanks.
Thnx mate … you are a life savior :)