corradomatt
Member
Posted 5 months ago #
I'm trying to display the child posts on a single CPT page and the output is very strange.
I'm using the following code at the end of the post loop...
<?php
$child_posts = types_child_posts('puppies');
foreach ($child_posts as $child_post) {
echo $child_post->post_title;
echo $child_post->fields['color'];
}
?>
It works to display the post_title perfectly fine but it won't display the 'color' field value that I defined for the child posts. Instead it displays 'Array' and I'm not sure why it's doing this....any suggestions?
Also, is there a way to display the child posts with html like we can for the Types Fields (aka 'output' => 'html')?
http://wordpress.org/extend/plugins/types/
Srdjan
Member
Plugin Author
Posted 5 months ago #
Can you try adding prefix 'wpcf-' if field is created with Types plugin?
Is that field with multiple values or single value (non-repetitive field)?
corradomatt
Member
Posted 5 months ago #
Thanks for the reply Srdjan. I changed the last line of code from above to...
echo $child_post->fields['wpcf-color'];
And now it doesn't output anything. The field is a 1 line text field, non-repetitive.
Srdjan
Member
Plugin Author
Posted 5 months ago #
Can you create some debug output so we can see what is in there?
print_r($child_post->fields);
Let me try to help you by explaining what's happening. I hope that this will be enough to set you on the right course.
types_child_posts will return the child posts, but you need to have the correct name of the custom field. It's explained, with an example here:
http://wp-types.com/documentation/user-guides/querying-and-displaying-child-posts/
Types custom fields are created with the wpcf- prefix by default. However, this may change if you associated an existing custom fields with Types control.
You should approach this task in steps. First, see what the output from the types_child_posts call, as Srdjan suggested. Then, you can see how to display it.
corradomatt
Member
Posted 5 months ago #
Thank you for your reply Srdjan and Amir. I used the print_r command to view the output as suggested and this is what is returned...
Array ( [dob] => Array ( [0] => 1342310400 ) [eye-color] => Array ( [0] => ) [color] => Array ( [0] => Black/Brown Parti ) [pending] => Array ( ) [sex] => Array ( ) [date-available] => Array ( ) )
I've looked through the code examples on that link and other pages throughout the wp-types website for help and I'm stumped. I'm not a php expert or anything but I can usually figure out issues like this....I'm just not having any luck this time.
corradomatt
Member
Posted 5 months ago #
Ok, I got it. I was missing the [0] operator for the array to tell it what part of the resulting array I needed (even though there was only 1 option).
So in my case, I want the "color" field on the post type I'm using so my code was this...
echo $child_post->fields['color'];
But should have been this...
echo $child_post->fields['color'][0];
Hopefully that will help out someone else in the future. Thanks again guys...btw, great plugin!
waynmeyer
Member
Posted 3 months ago #
corradomatt you truly are the man, thanx for this post u saved me so much time. really appreciate it :) i have one more question i think you can answer, how do i add a class and add
views of the repeated fields?
Any help will be greatly appreciated
waynmeyer
Member
Posted 3 months ago #
Hey guys any help please? really need it ;)
corradomatt
Member
Posted 3 months ago #
Hi waynmeyer,
I actually went a different route with my output. Here is some sample code that I used....
Use this link to see the code.... http://pastie.org/6136066
I hope that helps.