Support » Fixing WordPress » How to enter multiple values for a key using a comma?

  • Resolved dguyen

    (@dguyen)


    Okay, this should be fairly simple for most of you gurus. I am making use of metaboxes and custom fields just fine. The dilemma is, how do I enter multiple values per key using a comma, semicolon, or colon…etc… and display those values separately.

    Example:

    Key: Grocery list
    Values: Meat, Cheese, Bread, Wine

    I would rather not have to choose “Grocery List” then type in one value, then do it all over again. I would rather choose “Grocery List” from the existing Keys and comma separate the values.

    Righ now I’m using the get_post_meta function for multiple values. It sounds like a implode type function? Please, much help appreciated. Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • If you were to repeat your custom field name.
    get_post_custom_values('custom_field') “This function is useful if you wish to access a custom field that is not unique, i.e. has more than 1 value associated with it”
    http://codex.wordpress.org/Function_Reference/get_post_custom_values

    If you want to use the custom field name just once with comma separated list. Then you have to explode the value on the comma into an array.

    In the loop:

    $value = implode(get_post_custom_values('custom_field'));
    $array = explode(',',$value);

    Or

    $value = get_post_meta($post-ID, 'custom_field', true);
    $array = explode(',',$value);

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

    I haven’t tested these. Though I am using the get_post_custom_values with implode in a theme right now.

    Thread Starter dguyen

    (@dguyen)

    Thanks for the response… Hmmm… I’m getting an invalid argument do to my use of foreach -here is the code I’m using:

    <?php $figures = get_post_meta($post->ID, 'example-images', false);
    	foreach ( $figures as $figure ) { ?>
    	<div class="wp-caption"><a href="<?php echo $figure; ?>"><img width="200" src="<?php echo $figure; ?>"</a><p class="wp-caption-text">Figure 3b</p></div>
    	<?php } ?>

    Key: example-images
    Value: “URL of image”

    In my how-to posts, I display the picture examples along side the content. The pictures show up fine as long as I enter each image’s URL separately. I would like to enter many URLs separated by comma. Rather than one at a time. I hope I’ve explained myself correctly. Using the Key one time is fine with me as long as I can use comma separated values. Thanks again for your time.

    if you are using your comma separated list:

    <?php $figures = explode(',',get_post_meta($post->ID, 'example-images', true));
         if($figures) {
             foreach ( $figures as $figure ) { ?>
    	<div class="wp-caption"><a href="<?php echo $figure; ?>"><img width="200" src="<?php echo $figure; ?>"></a><p class="wp-caption-text">Figure 3b</p></div>
    	<?php }
          } ?>

    this will give an error message if the customfield is empty, which can be avoided by wrapping the foreach into a conditional if($figures) as done in the above code; also avoids empty divs.

    Thread Starter dguyen

    (@dguyen)

    AWESOME works like a charm, you’re the man! Duh… solutions always seem so simple after the fact. Thanks so much!

    how to do same thing for

    • ? means if I want to use More Field plugin and want to add separate list for each entry separated by comma? like
      <ul>
      <li>Mylistone</li>
      <li>Mylisttwo</li>
      <li>so on...</li>
      </ul>

      but user can enter in More Field input like (Mylistone, Mylisttwo etc)

      si it will appear like bellow

    • Mylistone
    • Mylisttwo
    • so on…
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to enter multiple values for a key using a comma?’ is closed to new replies.