3

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?

1 Answer 1

2

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>";
2
  • 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
    Commented Mar 24, 2013 at 10:38
  • 1
    I guess you mean that you will add the custom fields in the backend, I updated the answer.
    – birgire
    Commented Mar 24, 2013 at 11:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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