WordPress.org

Ready to get started?Download WordPress

Forums

Custom field, multiple values (9 posts)

  1. azezer
    Member
    Posted 4 years ago #

    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

  2. stvwlf
    Member
    Posted 4 years ago #

    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>';

  3. azezer
    Member
    Posted 4 years ago #

    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 ?.

  4. azezer
    Member
    Posted 4 years ago #

    Anybody who has a tip where i can read about this and more?

    Azezer

  5. stvwlf
    Member
    Posted 4 years ago #

    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.

  6. azezer
    Member
    Posted 4 years ago #

    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

  7. azezer
    Member
    Posted 4 years ago #

    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

  8. azezer
    Member
    Posted 4 years ago #

    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

    Do i miss something so that the server gets that it is a image to fetch?

    Azezer

  9. azezer
    Member
    Posted 4 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic