Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Echo values for custom fields with the same ‘key’ value

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #31181
    cre8tive1
    Member

    Hi all,

    I’m working on my first WP site that uses custom fields. I’ve created a custom field titled “Testimonials”. I have added three instances of that custom field to this page http://snorkel.jerryrossphotography.com/dolphins/

    I’m trying to display all 3 instances of the “Testimonial” field in the sidebar. However, I can only get 1 to display.

    Any suggestions?

    Below is the code within my sidebar.

    
    /*
    Template Name: Testimonials
    */
    ?>
    #67167
    gno
    Member

    I have not worked alot with wordpress – at all. So I might be completely wrong…

    But if it was just plain PHP, I’d say that you miss some kind of loop. A while or foreach loop would do the job.

    You only make one call to the get_post_meta function. I could imagine that there would be a function named get_post_meta_all, or something like it, that returned an array with all the values from the custom field of your choice…

    I hope you’ll figure something out :-)

    #67059
    jamygolden
    Moderator

    Try this – I haven’t tested it though:



    ID, 'Testimonial', true) ) : ?>
    ">





    #67064
    cre8tive1
    Member

    Based on the above, I modified it to echo out the value of the ‘Testimonial’ custom field. However, I’m still only getting the first instance of ‘Testimonial’ to display. The page below has 3 instances of the custom field.

    http://snorkel.jerryrossphotography.com/dolphins/



    ID, 'Testimonial', true) ) : ?>






    #67071
    gno
    Member

    Im back again after a good nights sleep. I’m sorry, but I was too lazy to look this up last night.

    A quick view in the wordpress documentation revealed that my suspicion was right.

    You should not use get_post_meta(), but get_post_custom_values(). It does the same thing, but is for when you have customfields with non unique keys. It returns an array that you can throw in a foreach.

    Link to the manual page: http://codex.wordpress.org/Function_Reference/get_post_custom_values

    Example, build on the code from the OP:



    EDIT: (I have no idea why it screwed the coloring and indexing of the code above…)

    #67003
    cre8tive1
    Member

    Perfect. That worked. I really need to improve my php skills. I had read that reference in your link multiple times, but couldn’t figure out the syntax.

    Thanks for the help!

    #67004
    gno
    Member

    Your welcome. Glad it worked. :-)

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Back End’ is closed to new topics and replies.