- This topic is empty.
-
AuthorPosts
-
September 5, 2011 at 1:51 pm #34224
cybershot
ParticipantI am trying to get the recent comments from wordpress but I keep getting an error
$recent_comments = get_comments( array(
'number' => 5,
'status' => 'approve'
) );
foreach ( $recent_comments as $comment) {
echo $comment;
}
I am getting this error and don’t know what it means
Object of class stdClass could not be converted to string
September 5, 2011 at 3:03 pm #86431Bob
MemberDoes the error show anything else? Maybe on what line the error is found? Usually it does that I think, maybe you can show us that line of code as well?
September 5, 2011 at 3:12 pm #86433chrisburton
Participant$args = array(
'status' => 'approve',
'number' => '5',
);
$comments = get_comments($args);
foreach($comments as $comment) :
echo($comment->comment_author . '
' . $comment->comment_content);
endforeach;September 5, 2011 at 5:49 pm #86437cybershot
Participantyes that worked christopher. I don’t recognize this kind of code
echo($comment->comment_author . '
' . $comment->comment_content);
I have never seen the code inside brackets for an echo before. So this is interesting.
September 5, 2011 at 5:57 pm #86438chrisburton
ParticipantWhat the above code will show is who wrote the comment and then display the comment on the next line. However, I posted a link to what you don’t understand as it explains it more easily than I could.
September 6, 2011 at 12:32 am #86470kalps1982
MemberIts simple, you are trying to print STDClass object to string. Normally object class can not be converted to string as it has properties assigned to it.
i.e.
$str = “this is string”;
$object = new stdClass();
$object->test = “string in object”.If you do
echo $str; // it would print “this is string”
but if you do
echo $object; it would put error as you are getting on in comment list.
but if you do
echo $object->test; // it would print “string in object”
I hope you understood what i explained. You can also check how to get comments from wordpress with get_comments at
http://codex.wordpress.org/Function_Reference/get_comments
here they have mentioned “(Array) Comment objects with the following indexes:”
Regards,
Kalpesh Patel (http://www.think-1st.com) -
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.