Forums

[resolved] Passing parameter to get_comments function (7 posts)

  1. savegothamcity
    Member
    Posted 2 years ago #

    I'm trying set the value "post_id" to the ID of the current post and pass it into the get_comments() function. I tried the following but it doesn't work. Can anybody tell me what I'm doing wrong?

    <?php global $post; echo $post->ID; ?>
    <?php
    $comments = get_comments('post_id=$post->ID');
    foreach($comments as $comm) :
    echo($comm->comment_author);
    echo($comm->comment_date);
    echo($comm->comment_content);
    endforeach;
    ?>

    Thanks in advance!

  2. Mark / t31os
    Moderator
    Posted 2 years ago #

    $comments = get_comments($post->ID);

  3. savegothamcity
    Member
    Posted 2 years ago #

    Thanks for the reply. I just tried $comments = get_comments($post->ID); and it did not work as it returns all comments for all posts.

    I only want to get the comments for a specific post. When I try $comments = get_comments('post_id=24'); I get all the comments just for post 24. How do I get it to work dynamically where I can it to return the comments just for the current post?

  4. savegothamcity
    Member
    Posted 2 years ago #

    Anybody out there have any suggestions? I'm really struggling with this one.

  5. Mark / t31os
    Moderator
    Posted 2 years ago #

    Can you show the complete code for the file where you're placing this... i'll then test and try to replicate...

  6. montanamax
    Member
    Posted 2 years ago #

    I just had a similar problem tonight when trying take action if there were approved comments for a post. Solved it like this:

    <? php
    
    $comments_array = get_comments('post_id=' . $id . ',status=approve');
    $comments_approved  = count($comments_array);
    ?>
    
    <?php if (0 != $comments_approved){ ?> <code><em>blah blah</em></code><?php } ?>
  7. leonl
    Member
    Posted 2 years ago #

    I solved this problem like this:

    $comments = get_comments('number=6&status=approve&post_id=$id');
      foreach($comments as $comm) :
        echo '<li>';
    	echo($comm->comment_author);
    	echo ':';
    	echo($comm->comment_content);
      	echo '</li>';
    endforeach;

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.