Title: Custom field, multiple values
Last modified: August 19, 2016

---

# Custom field, multiple values

 *  [azezer](https://wordpress.org/support/users/azezer/)
 * (@azezer)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/custom-field-multiple-values/)
 * Hi
 * Im pretty new to this with <?php, but working to learn, and i cant figure this
   out and would apprichiate som help.
 * **Here goes the original code with one value i get working:**
 *     ```
       <?php
       $description = get_post_meta($post->ID, "description", false);
   
       if ($description[0]=="") { ?>
   
       <!-- If there are no custom fields, show nothing -->
   
       <?php } else { ?>
   
       <div class="wrap">
       	<h3>Products</h3>
   
       	<?php foreach($description as $description) {
       	echo '<p>'.$description.'</p>';
       	} ?>
   
       </div>
   
       <?php } ?>
       ```
   
 * I have difficulties to modify this code to retrive values from more then one 
   custom field input and putting it out together and inbetween the same <html>.
 * **Here is code with one more value added, but im uncearten how to combine thoose
   2 values:**
 *     ```
       <?php
       $description = get_post_meta($post->ID, "Description", false);
       $seccond_value = get_post_meta($post->ID, "price", false);
   
       if ($description[0]=="") { ?> <strong>// how do i combine 2 values here?</strong>
   
       <!-- If there are no custom fields, show nothing -->
   
       <?php } else { ?>
   
       <div class="wrap">
       	<h3>products</h3>
   
       	<?php foreach($description as $description) {
       	echo '<p>'.$description.'</p>';
       	} ?> <strong>// how does this part work and how to i combine 2 values here?</strong>
   
       </div>
   
       <?php } ?>
       ```
   
 * Maybe someone here has a good tip for a good sajt that covers my questions above?
 * // Azezer

Viewing 8 replies - 1 through 8 (of 8 total)

 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/custom-field-multiple-values/#post-1166677)
 * `if ($description[0]=="") { ?> <strong>// how do i combine 2 values here?</strong
   >`
 * that line can stay as it is – you don’t need to test for price since a price 
   could be zero.
 * change the output line to
    `echo '<p>'.$description.'&nbsp;&nbsp;&nbsp;'.$seccond_value.'
   </p>';`
 *  Thread Starter [azezer](https://wordpress.org/support/users/azezer/)
 * (@azezer)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/custom-field-multiple-values/#post-1166686)
 * Thank u!
 * Now i got a output of that seccond_value.
 * But still only if i have a true statement of that value.
 * I guess that a need to do that foreach statement for the seccond value as well,
   but how do i combine theese two, or three … for that matter.
 * `<?php foreach($description as $description)` _// how to add $seccond\_value 
   here?_
 * Another question that popped up is if i can combine two $values if i always wants
   the to be together.
 * Like description and price.
 *     ```
       $description
         $price
       ```
   
 * I mean to save code space and only declare thoose two one time, nd then have 
   1 combined vale like $product ?.
 *  Thread Starter [azezer](https://wordpress.org/support/users/azezer/)
 * (@azezer)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/custom-field-multiple-values/#post-1166817)
 * Anybody who has a tip where i can read about this and more?
 * Azezer
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/custom-field-multiple-values/#post-1166824)
 * hi
 * I would combine them like this in one custom field called Product, separated 
   by an ampersand
    “A nice blue one&23.95”
 * $products = get_post_meta($post->ID, “Product”, false);
 * within the display code:
 *     ```
       <?php foreach($products as $product) {
   
         $temp = explode("&", "$product");
         $descrip = $temp{0];
         $price = $temp{1];
   
         echo '<p>'.$descrip.'&nbsp;&nbsp;&nbsp;'.$price.'</p>';
   
       ?>
       ```
   
 * “explode” takes the custom field value and breaks it into 2 using the ampersand
   as the separator, creating an array of two elements. Then assign element 1 to
   a descrip variable, and element 2 to a price variable. (The array elements start
   numbering at zero, which is why the 2nd element is element [1] )
 * The problem with using two custom fields that have multiple occurances is you
   have no guarantee of the order in which WP will return them in the resulting 
   array from get_post_meta. In other words, in terms of the order in which the 
   items were originally entered, if there are 3 of them, the descrip could be returned
   in the order 1 – 3 – 2 and the price in the order 2 – 1 – 3 Thus the prices wouldn’t
   match up with the descriptions. When you combine them in one field you guarantee
   that won’t happen.
 *  Thread Starter [azezer](https://wordpress.org/support/users/azezer/)
 * (@azezer)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/custom-field-multiple-values/#post-1166828)
 * Thanks stvwlf.
 * works like a charm, found som minor errors in the code like, { instead of [ at
   the temp variable.
 * foreach($products as $product // why do we say, $products as $product ?
    Is it
   so that $ products is a multiple value and we want to declare that we can use
   it as $product several times?
 * Now i want to implement a <img> above each Product.
 * Will try to do this when i get home from work, maybe i can manage that myself.
 * Azezer
 *  Thread Starter [azezer](https://wordpress.org/support/users/azezer/)
 * (@azezer)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/custom-field-multiple-values/#post-1166832)
 * I think i can use something like this:
    `"<img src='" . $img . "' />"`
 * to implemate in this:
 *     ```
       <?php
       $products = get_post_meta($post->ID, "product", false); 
   
       if ($products[0]=="") { ?>
   
       <!-- If there are no custom fields, show nothing -->
   
       <?php } else { ?>
   
       <div class="produktbeskrivning">
       	<h3>Products</h3>
   
       <?php foreach($products as $product) {
   
       $temp = explode("&", "$product");
         $descrip = $temp[0];
         $price = $temp[1];
         $img = $temp[2];
   
       echo '<div class="grid">'.$descrip.'&nbsp;&nbsp;&nbsp;'.$price.'</div>'; } ?>
   
       </div>
   
       <?php } ?>
       ```
   
 * Ive tried to put in in after $price, but it gave me error ” blank page “.
 * search continues
 *  Thread Starter [azezer](https://wordpress.org/support/users/azezer/)
 * (@azezer)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/custom-field-multiple-values/#post-1166833)
 * Now i continued a bit and got this far:
 *     ```
       <?php
       $products = get_post_meta($post->ID, "product", false); 
   
       if ($products[0]=="") { ?>
   
       <!-- If there are no custom fields, show nothing -->
   
       <?php } else { ?>
   
       <div class="produktbeskrivning">
       	<h3>Products</h3>
   
       <?php foreach($products as $product) {
   
       $temp = explode("&", "$product");
         $descrip = $temp[0];
         $price = $temp[1];
         $img = $temp[2];
   
       echo '<div class="grid">'.$descrip.'&nbsp;&nbsp;&nbsp;'.$price.'&nbsp;&nbsp;&nbsp;<img src"'.$img.'" /></div>'; } ?>
   
       </div>
   
       <?php } ?>
       ```
   
 * But i cant get the image to show.
 * I print this in custom field product:
 * Product&299:-&[http://xxxxxx.com/wp-content/uploads/image.jpg](http://xxxxxx.com/wp-content/uploads/image.jpg)
 * Do i miss something so that the server gets that it is a image to fetch?
 * Azezer
 *  Thread Starter [azezer](https://wordpress.org/support/users/azezer/)
 * (@azezer)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/custom-field-multiple-values/#post-1166842)
 * I finally saw the error, i forgot a = after src at the image.
 * The code works =) sweet.
 * Now to implement the Timtumb into this to, but thats another story.
 * Thanks for help stvwlf.
 * Tread solved
 * //Azezer

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Custom field, multiple values’ is closed to new replies.

## Tags

 * [custom field](https://wordpress.org/support/topic-tag/custom-field/)

 * In: [Everything else WordPress](https://wordpress.org/support/forum/miscellaneous/)
 * 8 replies
 * 2 participants
 * Last reply from: [azezer](https://wordpress.org/support/users/azezer/)
 * Last activity: [16 years, 10 months ago](https://wordpress.org/support/topic/custom-field-multiple-values/#post-1166842)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
