WordPress Comments

Beginner
6 min read
Updated: Nov 2026
2 examples
Comment loop

Definition

In WordPress, a comment is a message that a visitor leaves under a post. Your theme shows the comments with a file called comments.php. Inside it you run the comment loop, which means you go through every comment of the post and print it.

The easy way is wp_list_comments(). It prints all comments, with replies and avatars. Show the form with comment_form(). Show a photo with get_avatar().

Where to use them.

Most comment tags work only for the current comment, so use them in a wp_list_comments() callback. Count and link tags, such as comments_number(), go in the post loop. Load comments.php from single.php with comments_template().

Syntax

A very small comments.php file looks like this:

WordPress
<?php // single.php ?>
<?php comments_template(); ?>

<?php // comments.php ?>
<?php if ( have_comments() ) : ?>
  <ol><?php wp_list_comments(); ?></ol>
<?php endif; ?>
<?php comment_form(); ?>

wp_list_comments() vs Single Comment Tags

Both ways show comments. They differ in how much HTML you control.

WayBest forHTML controlExample
wp_list_comments()The whole list, with replies, classes, and avatars.Little. Change it with options.wp_list_comments( array( 'avatar_size' => 48 ) );
wp_list_comments() + callbackYour own HTML, with WordPress still handling the nesting.Full control of each comment.'callback' => 'mytheme_comment'
Single tagsPrinting one piece of data, such as a name, a date, or a message.Full control.comment_author();
get_avatar()Showing the commenter's photo.Returns an img tag. Use echo.echo get_avatar( $comment, 48 );

Function Groups at a Glance

Pick the group that matches the job, then open the guide you need.

GroupTypical jobStart here
Comment author & identityShow who wrote a comment: name, email, website, IP address, and avatar.comment_author(), comment_author_link(), comment_author_email(), comment_author_email_link(), comment_author_url()
Comment content & metaShow the message, date, time, ID, type, and CSS classes of a comment.comment_text(), comment_excerpt(), comment_date(), comment_time(), comment_ID()
Reply & formBuild the comment form and the Reply and Cancel links.comment_form(), comment_form_title(), comment_reply_link(), cancel_comment_reply_link(), comment_id_fields()
Counts, links & navigationShow counts, link to comments, list all comments, and split them into pages.comments_number(), comments_link(), comments_popup_link(), wp_list_comments(), next_comments_link()

WordPress Examples

Two short theme examples with sample output.

Example 1 — A basic comments.php

Show the count, the list of comments, and the form.

WordPress
<?php if ( have_comments() ) : ?>
  <h2><?php comments_number( 'No comments', 'One comment', '% comments' ); ?></h2>
  <ol class="comment-list">
    <?php wp_list_comments( array( 'style' => 'ol', 'avatar_size' => 48 ) ); ?>
  </ol>
<?php endif; ?>

<?php comment_form(); ?>

How It Works

  1. have_comments() checks that the post has comments.
  2. wp_list_comments() prints one li for each comment, with avatars.
  3. comment_form() prints the form after the list.

Example 2 — A custom callback

Use single comment tags to build your own HTML for each comment.

WordPress
function mytheme_comment( $comment, $args, $depth ) { ?>
  <li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
    <?php echo get_avatar( $comment, 48 ); ?>
    <strong><?php comment_author(); ?></strong>
    <small><?php comment_date(); ?></small>
    <?php comment_text(); ?>
    <?php
    comment_reply_link( array_merge( $args, array(
      'depth'     => $depth,
      'max_depth' => $args['max_depth'],
    ) ) );
    ?>
<?php }

// In comments.php:
wp_list_comments( array( 'callback' => 'mytheme_comment' ) );

How It Works

  1. WordPress calls your function once for each comment.
  2. comment_author(), comment_date(), and comment_text() print the data of that comment.
  3. get_avatar() returns the image, so it needs echo. The walker closes the li for you.

Next: WordPress Comment Functions Index

After you know the basics, open the functions index for all 30 guides.

Full list: WordPress Comment Functions index (/wordpress/comment/function).

Frequently Asked Questions

It is the theme file that shows the comments of a post and the comment form. WordPress loads it when you call comments_template() in single.php.
It is the list of comments of a post. wp_list_comments() runs it for you and prints one item for each comment.
Use it for the main list. It handles nested replies, classes, and avatars. Add a callback if you want your own HTML.
Use tags such as comment_author() and comment_text() inside a callback, or in a custom loop where you want full control of the HTML.
Use echo get_avatar( $comment, 48 ). It returns the img tag, so you must echo it.
Call comment_form() after the list of comments. It prints all the fields for you.

Browse every comment function

Open the functions index for simple guides with examples and FAQs.

Comment Functions Index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful