Treehouse: Grow your CSS skills. Land your dream job.

listing comments in wordpress

  • # September 5, 2011 at 1:51 pm

    I 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

    Does 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


    $args = array(
    'status' => 'approve',
    'number' => '5',
    $comments = get_comments($args);
    foreach($comments as $comment) :
    echo($comment->comment_author . '
    ' . $comment->comment_content);
    # September 5, 2011 at 5:49 pm

    yes 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

    What 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

    Its 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.


    $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

    here they have mentioned “(Array) Comment objects with the following indexes:”

    Kalpesh Patel (

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.