Showing only the last 3 comments for a post / page (2 posts)

  1. Jon W
    Posted 2 years ago #

    For my mobile theme I wish to show only the last 3 comments for that post (or page) and the new comment form.

    I added this code to the comments.php

    <?php $args = array(
        'number' => 3,
        'post_id' => $post->ID,
        'status' => 'approve'
    $latest_comment = get_comments( $args );
    if( $latest_comment ) foreach( $latest_comment as $comment ) { ?>
    <?php echo $comment->comment_author; ?>: <?php echo wp_html_excerpt( $comment->comment_content, 500 ); ?>..."<br /><br />
    <?php } ?>
    To read all questions and answers view the website on a desktop or tablet.
    <b>Ask a Question / Leave a Comment:</b>
    <?php comment_form(array('comment_notes_after' => '')); ?>

    However, this shows the last 3 comments for the whole site, not for that page. Better than showing all comments (some pages have hundreds) but not what I wanted to do.

    Does anybody know how to get the current page comments to show?

    Many thanks


  2. ubaidullahbutt
    Posted 2 years ago #

    I think you are put the code in your single.php.

    Then use loop for this, like

    $post_obj = new WP_Query($args);
    while($post_obj->have_posts() ) : $post_obj->the_post();
        //display comments
        $comments = get_comments(array(
            'post_id' => $post->ID,
            'number' => '2' ));
        foreach($comments as $comment) {
            //format comments

    Also Check out this, in case you want comments from specific post.


Topic Closed

This topic has been closed to new replies.

About this Topic