- This topic is empty.
-
AuthorPosts
-
January 7, 2011 at 8:43 pm #31181
cre8tive1
MemberHi 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
*/
?>
January 7, 2011 at 10:10 pm #67167gno
MemberI 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 :-)
January 8, 2011 at 2:08 am #67059jamygolden
MemberTry this – I haven’t tested it though:
ID, 'Testimonial', true) ) : ?>
">
January 8, 2011 at 8:54 am #67064cre8tive1
MemberBased 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) ) : ?>
January 8, 2011 at 11:36 am #67071gno
MemberIm 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…)
January 8, 2011 at 4:27 pm #67003cre8tive1
MemberPerfect. 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!
January 8, 2011 at 4:37 pm #67004gno
MemberYour welcome. Glad it worked. :-)
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.