Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I use these codes to print custom field query. My custom field key is out_wiki

    <?php if( get_post_meta($post->ID, "out_wiki", true) ): ?>

        <div class="outlink">
            <a href="http://en.wikipedia.org/w/index.php?search=<?php echo get_post_meta($post->ID, "out_wiki", true); ?>" target="_blank">
                <img src="http://www.wikipedia.com/favicon.ico" title="Wikipedia title">
            </a>
        </div>

    <?php endif; ?>

I want to store multiple values in same custom field key and print them at once. How can I do that?

share|improve this question

1 Answer 1

up vote 2 down vote accepted

to store multiple values in same custom field key and print them at once

If you want to store it as site option, you can use update_option() :

http://codex.wordpress.org/Function_Reference/update_option

Example 1:

// some array to store:
$items=array('yellow','orange','green');

// save the array
update_option('myitems',$items);

// get the array 
$items=get_option('myitems');

// print the array
echo "<ul>";
foreach($items as $item){
    echo "<li>".$item."</li>";
}
echo "</ul>";

If you want to store it as post meta (i.e. for each post), you can use update_post_meta()

http://codex.wordpress.org/Function_Reference/update_post_meta

Example 2:

// some array to store:
$items=array('yellow','orange','green');

// save the array
update_post_meta($post_id,'myitems',$items);

// get the array
$items = get_post_meta($post_id,'myitems',true);

// print the array
echo "<ul>";
foreach($items as $item){
    echo "<li>".$item."</li>";
}
echo "</ul>";

Example 3:

If you want to add the custom fields (same meta key) and values from the backend like this:

enter image description here

you can retreieve the values like this:

// get the array for current post
$items = get_post_meta($post->ID,'myitems'); // we skip the true part here

// print the array
echo "<ul>";
foreach($items as $item){
    echo "<li>".$item."</li>";
}
echo "</ul>";
share|improve this answer
    
I'm novice for php. I'll use this in wordpress with custom fields. So how can I use it in wordpress custom fields. –  Imrahil Mar 24 '13 at 10:38
1  
I guess you mean that you will add the custom fields in the backend, I updated the answer. –  birgire Mar 24 '13 at 11:01
    
Thank you so much. Example 3 is what I'm looking for. –  Imrahil Mar 24 '13 at 14:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.