Customize comment list markup

You should use wp_list_comments(), because this will call a Walker class that can handle comment threads:

// from wp_list_comments()
if ( empty($walker) )
    $walker = new Walker_Comment;

$walker->paged_walk($_comments, $max_depth, $page, $per_page, $r);

Walker_Comment handles the markup for the comments. You can pass a custom walker to change it.

Example:

class Simple_Comment_Walker extends Walker_Comment
{
    function start_el( &$output, $comment, $depth, $args, $id = 0 ) {
        $depth++;
        $GLOBALS['comment_depth'] = $depth;
        $GLOBALS['comment'] = $comment;

        print '<li>' . get_comment_author_link() . '<br>';
        comment_text();
    }
}

wp_list_comments( array ( 'walker' => new Simple_Comment_Walker ) );

But this is not needed if you want to change the single comment markup only. You can pass a callback parameter to wp_list_comments() to do the same:

function simple_callback() 
{
    print '<li>' . get_comment_author_link() . '<br>';
    comment_text();
}
wp_list_comments( array ( 'callback' => 'simple_callback' ) );
  • Use a custom walker if you want to change the container elements for the comment list too, use a callback if you want to change single comments only.

  • You can overwrite everything in Walker_Comment, see wp-includes/comment-template.php for details.

  • Keep in mind you should not close the first comment container (in my examples the <li>), the Walker does this when the whole thread has been printed.

Leave a Comment