WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. Jon W
    Member
    Posted 1 year 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 } ?>
    </p>
    <p>
    To read all questions and answers view the website on a desktop or tablet.
    </p>
    <p>
    <b>Ask a Question / Leave a Comment:</b>
    </p>
    <?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

    Jon.

  2. ubaidullahbutt
    Member
    Posted 1 year 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
        }
    endwhile;

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

    http://codex.wordpress.org/Function_Reference/wp_list_comments#Display_Comments_for_a_Specific_Page.2FPost

Topic Closed

This topic has been closed to new replies.

About this Topic