Forums

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

Home Forums Back End listing comments in wordpress

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #34224
    cybershot
    Participant

    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

    #86431
    Bob
    Member

    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?

    #86433
    chrisburton
    Participant

    @cybershot

    $args = array(
    'status' => 'approve',
    'number' => '5',
    );
    $comments = get_comments($args);
    foreach($comments as $comment) :
    echo($comment->comment_author . '
    ' . $comment->comment_content);
    endforeach;
    #86437
    cybershot
    Participant

    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.

    #86438
    chrisburton
    Participant

    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.

    http://php.net/manual/en/language.operators.string.php

    #86470
    kalps1982
    Member

    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.

    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)

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