WordPress wp_list_comments(): Build a Custom Comment Card Template with Reply Link and Avatar

Custom comment markup with a wp_list_comments() callback

wp_list_comments() prints the comments of a post with WordPress’ default markup: an <li> per comment with the avatar, author, date and text in a fixed structure. A design with comment cards, a “3 days ago” date or a different order of elements needs its own markup. The callback argument gives full control over each comment, while WordPress keeps handling threading, pagination and reply links.

Problem: A theme shows comments as cards with a round avatar, the author’s name, a relative date, and edit and reply links, which the default markup cannot do.

Solution: Pass a callback to wp_list_comments() that prints the opening element and the content of one comment, and leave the closing tag to WordPress (or set end-callback when the opening element is not an <li>).

The template

In comments.php:

<?php if ( have_comments() ) : ?>
    <ol class="comment-list">
        <?php
        wp_list_comments( array(
            'style'       => 'ol',
            'callback'    => 'ha_comment',
            'avatar_size' => 48,
            'short_ping'  => true,
        ) );
        ?>
    </ol>
    <?php the_comments_pagination(); ?>
<?php endif; ?>

<?php comment_form(); ?>

The callback

<?php
function ha_comment( $comment, $args, $depth ) {
    $time = sprintf(
        /* translators: %s: time difference, e.g. "3 days" */
        __( '%s ago', 'textdomain' ),
        human_time_diff( get_comment_time( 'U', true, $comment ), time() )
    );
    ?>
    <li id="comment-<?php comment_ID(); ?>" <?php comment_class( 'comment-card', $comment ); ?>>
        <article class="comment-card__inner">
            <div class="comment-card__avatar"><?php echo get_avatar( $comment, $args['avatar_size'] ); ?></div>

            <div class="comment-card__body">
                <header class="comment-card__meta">
                    <span class="comment-card__author"><?php comment_author_link( $comment ); ?></span>
                    <a class="comment-card__date" href="<?php echo esc_url( get_comment_link( $comment ) ); ?>">
                        <time datetime="<?php echo esc_attr( get_comment_time( 'c', true, $comment ) ); ?>"><?php echo esc_html( $time ); ?></time>
                    </a>
                </header>

                <?php if ( '0' === $comment->comment_approved ) : ?>
                    <p class="comment-card__moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'textdomain' ); ?></p>
                <?php endif; ?>

                <div class="comment-card__text"><?php comment_text( $comment ); ?></div>

                <footer class="comment-card__actions">
                    <?php
                    comment_reply_link( array_merge( $args, array(
                        'depth'     => $depth,
                        'max_depth' => $args['max_depth'],
                    ) ), $comment );
                    edit_comment_link( __( 'Edit', 'textdomain' ), ' · ' );
                    ?>
                </footer>
            </div>
        </article>
    <?php
    // no closing </li>: WordPress closes it after the replies of this comment
}

The callback prints only the opening <li>. Replies are printed by WordPress inside it, in a nested list, and then WordPress closes the <li>. If your callback closed it itself, replies would end up outside their parent and threading would break.

get_comment_time( 'U', true ) returns the GMT timestamp, which matches time(). Mixing current_time( 'timestamp' ) with the local comment time shifts the result by the site’s offset.

Details that keep it working

  • comment_class() adds classes such as depth-2, bypostauthor and byuser, which styling and the reply script rely on.
  • The id="comment-123" is needed for links to a comment and for the reply link to move the form under the right comment.
  • Load the reply script in functions.php: if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) { wp_enqueue_script( 'comment-reply' ); }.

NOTE: The moderation message is shown only to the comment’s author, right after posting: WordPress includes their unapproved comment for them (identified by a cookie) and hides it from everybody else. Keep that check in custom callbacks, or the author sees their comment without knowing it is not public yet.