Grow your CSS skills. Land your dream job.

get wordpress comments

  • # November 5, 2011 at 12:20 am

    I am trying to get post specific comments. The code below does return comments, but it gets them all. How can I get those specific to the post?




    < ?php
    //$args = array('order' => 'ASC');
    $comments = get_comments('$post_id');
    foreach($comments as $comment) :
    ?>


    • < ?php echo ($comment->comment_content); ?>



    < ?php endforeach; ?>

    # November 7, 2011 at 1:44 pm

    Well, I would probably just use the code from the twentyeleven theme and use the have_comments loop. But, if you want to go this way, you could grab the post ID first, then put it into get_comments. Something like this:

    $current = $post->id;

    Then you can use:

    get_comments($current);
    # November 7, 2011 at 2:06 pm

    Your code does not pass the correct parameter to the function. You care just getting all comments because of that.

    < ?php
    //$args = array('order' => 'ASC');
    $comments = get_comments('$post->id');
    foreach($comments as $comment) :
    ?>


    • < ?php echo ($comment->comment_content); ?>



    < ?php endforeach; ?>

    I changed the ‘$post_id to $post->id that should fix the error.

    # November 9, 2011 at 12:14 am

    I figured it out shortly after making this post. I used get_the_id to figure out the post id. For some reason when I try post->id it returns things badly. I am using the chunk of code above inside the loop. So this works well



    $id = get_the_ID();
    $args = array(
    'post_id' => $id,
    'order' => 'ASC'
    );
    $comments = get_comments($args);
    foreach($comments as $comment) :
    ?>


    • < ?php echo ($comment->comment_content); ?>



    < ?php
    endforeach;
    ?>



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

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".