1

I'm using this to allow me to display my comments for posts on category pages:

<?php
$wp_query->is_single = true;
comments_template();
$wp_query->is_single = false;
?>

However I don't want to display the comment form. Is there a way to display the comments only on a comment page?

1 Answer 1

1

Use get_comments() and pass the post ID as parameter. Then print the result as regular list. From a plugin I have published recently:

function t5_list_comments( $atts, $content = '' )
{
    '' !== $content && $content = wpautop( do_shortcode( $content ) );

    if ( ! isset ( $atts['post_id'] ) )
        return 'Please pass an argument for "post_id"';

    if ( ! $comments = get_comments( $atts ) )
        return 'No comments found for post ID '. esc_html( $atts['post_id'] );

    $out = $content . '<ul class="t5-comment-list">';
    foreach ( $comments as $comment )
    {
        $out .= sprintf(
            '<li>%1$s - %2$s %3$s</li>',
            get_comment_author_link( $comment->comment_ID ),
            get_comment_date( 'd.m.Y H:i', $comment->comment_ID ),
            wpautop( $comment->comment_content )
        );
    }

    return $out . '</ul>';
}

You can use it like this:

print t5_list_comments( array ( 'post_id' => get_the_ID() ) );
2
  • this works great, just can't figure out how to format the comment date using wpautop( $comment->comment_date). Any idea?
    – JonnyPlow
    Commented Jan 15, 2013 at 21:09
  • @JonnyPlow See my update.
    – fuxia
    Commented Jan 15, 2013 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.