• Resolved ljmyers

    (@ljmyers)


    Hello,

    Yes, I am yet another web monkey who knows just enough php to be dangerous and still learning about the powers of wordpress. FYI: I am using wordpress mu 2.8.4.

    I am trying to retrieve and show custom fields in my posts but the thing is . . . I want to retrieve them each individually and not all with one statement, allowing me more control over how they view in posts. I found this code in another topic and it works well but, I need to edit it so that I can use it once for each value that I want to retrieve instead of having it retrieve all values with one statement. Can anyone please help me?

    [code]

      <?php
      $custom_fields = get_post_custom($post->ID);
      $custom_field_keys = get_post_custom_keys();
      foreach ( $custom_field_keys as $key => $name ) {
      $valuet = trim($name);
      if ( '_' == $valuet{0} ){
      continue;
      }
      if ($name != "ratings_users" && $name != "ratings_score" && $name != "ratings_average"){
      echo "

    • ";
      echo "" . $name . ": ";
      $my_custom_field = $custom_fields[$name];
      foreach ( $my_custom_field as $key => $value )
      echo $value . "
    • ";
      }
      }
      ?>

    [/code]

    Thanks Bunches . . . Lana Banana

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter ljmyers

    (@ljmyers)

    Polite Bump ??

    What about get_post_meta?

    This function returns the values of the custom fields with the specified key from the specified post.

    Thread Starter ljmyers

    (@ljmyers)

    I tried that and it works great but it retrieves the keys and prints them in the post as well as the values. I was wanting to retrieve only the values by using individual php statements for each value. This way I could use tables, etc. in designing the output. Thanks for the suggestion though.

    I’ve searched and search for recourse code that I could get to work for me. I also tried the following code from the codex pages but I’m not knowledgeable enough in php to get it to work so needles to say, it won’t. lol

    [code]<?php

    $note_values = get_post_custom_values('note');
    foreach ( $note_values as $key => $value ) {
    echo "$key => $value ('note')";
    }

    ?>[/code]

    I get this as a result . . .

    [code]Warning: Invalid argument supplied for foreach() in /home/ljmyers/domains/reciperenaissance.com/public_html/wp-content/themes/citrus islandwp/single.php on line 11[/code]

    This is for multiple values per key anyway and I only have one value per key.

    Maybe a new WP query? I use this to print the contents of the custom field “test” from the latest post in the category “posts”. You need to be able to run PHP in posts using a plugin.

    <?php $my_query = new WP_Query('category_name=posts&showposts=1'); while ($my_query->have_posts()): $my_query->the_post(); ; echo get_post_meta($my_query->post->ID, "test", $single = true); ?><?php endwhile; endif; ?>

    Hmm, get_post_meta works for me in the loop in the WordPress Default theme’s index.php:

    <?php
    $value = get_post_meta($post->ID, 'cf1', true);
    echo 'this is value of cf1 key '.$value;
    ?>

    You’re right, that will work. No real need for a new query. I should have mentioned the reason behind me using new queries – I use a bunch of them on a landing page for generating links to the most recent published posts in different categories and previewing upcoming posts via custom fields.

    Thread Starter ljmyers

    (@ljmyers)

    Thanks for the help guys and my apologies for the delayed response. We spent the weekend at the farm.

    I have custom fields named(key) note, description and so on. Each has a value that will change with each post. How would I edit the following code to retrieve and print individually in my posts? If it helps, it is a recipe site. I know . . . “Not another one” LOL

    <?php
    $value = get_post_meta($post->ID, 'note', true);
    echo 'this is value of cf1 key '.$value;
    ?>
    <?php
    $value = get_post_meta($post->ID, 'description', true);
    echo 'this is value of cf1 key '.$value;
    ?>

    My apologies for my ignorance as well. I realize that I have much to learn about php.

    I would recommend something like this at the very top of your template file:

    <?php
    $note = get_post_meta($post->ID, 'note', true);
    $description = get_post_meta($post->ID, 'description', true);
    ?>

    With that, you can then echo out $note and $description interchangeably anywhere you want on that page. Should be very simple, so if you are still having problems, let me know.

    Thread Starter ljmyers

    (@ljmyers)

    I finally found something that works exactly how I want it to and thought I would post it here for anyone wanting to accomplish the same thing . . .

    <?php if(get_post_meta($post->ID, "note", true) || get_post_meta($post->ID, "description", true)): ?>
    
    <div style='margin-bottom:10px;'>
    <?php if(get_post_meta($post->ID, "note", true)): ?>
    <?php $key="note"; echo get_post_meta($post->ID, $key, true); ?>
    </div>
    <?php endif; ?>
    
    <div style='margin-bottom:10px;'>
    <?php if(get_post_meta($post->ID, "description", true)): ?>
    <?php $key="description"; echo get_post_meta($post->ID, $key, true); ?>
    <?php endif; ?>
    </div>
    
    <?php endif; ?>

    Thanks for the time spent helping me. ??

    Good to see that the three recommendations to use get_post_meta was followed. Thanks for the feedback. Marking resolved.

    Also see you marked this thread with the MU tag so should point out there is also an MU forum at https://mu.www.ads-software.com/forums/

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Retrieve Custom Fields Individually’ is closed to new replies.