WordPress comment_class() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Template tag

Definition

In WordPress, comment_class() prints a full class attribute for a comment, such as class="comment even thread-even depth-1".

These classes help you style odd and even rows, nested replies, comments from logged-in users, and comments by the post author.

Syntax

Place it inside the opening tag of the comment wrapper. Add your own class if you want.

WordPress
<li <?php comment_class(); ?>>
<li <?php comment_class( 'my-comment' ); ?>>
$classes = comment_class( '', null, null, false );
CallWhat it does
comment_class()Prints the class attribute for the current comment.
comment_class( 'my-comment' )Adds your own class to the list.
comment_class( '', null, null, false )Returns the class attribute instead of printing it.

Do not write class="" yourself. The function prints the whole attribute.

Full declaration
WordPress
comment_class( string|string[] $css_class = '', int|WP_Comment $comment = null, int|WP_Post $post = null, bool $display = true ): void|string

Parameters

ParameterDescription
$css_classOptional. One or more extra classes to add.
$commentOptional. A comment ID or WP_Comment object. Default is the current comment.
$postOptional. A post ID or WP_Post object. Default is the current post.
$displayOptional. true prints the attribute. false returns it. Default true.

Return Value

Return ValueDescription
void|stringPrints the class attribute when $display is true. Returns it as a string when $display is false.

WordPress Examples

Three short theme examples. Each one has a sample output.

Example 1 — Default classes

Add the standard comment classes to a list item.

WordPress
<li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">

How It Works

  1. comment marks a normal comment.
  2. even and thread-even help with striped rows.
  3. depth-1 is the nesting level.

Example 2 — Add your own class

Add a custom class to every comment.

WordPress
<article <?php comment_class( 'card' ); ?>>

How It Works

  1. The first argument is your own class.
  2. WordPress adds it to the standard classes.
  3. You can pass an array for more than one class.

Example 3 — Return instead of print

Use the fourth argument to get the attribute as a string.

WordPress
<?php $attr = comment_class( 'card', null, null, false ); ?>
<div <?php echo $attr; ?>>

How It Works

  1. The fourth argument false stops printing.
  2. The function returns class="..." as a string.
  3. You can then echo it where you need it.

Frequently Asked Questions

It prints a class attribute with classes such as comment, even or odd, and depth-1.
No. The function prints the full class attribute.
WordPress adds bypostauthor when the comment was written by the author of the post.
Pass it as the first argument, for example comment_class( 'my-comment' ).
Use get_comment_class(). It returns an array of class names.

Next: comment_date()

Print the date when the current comment was posted.

comment_date() tutorial →

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.

9 people found this page helpful