comment_class()WP 2.7.0

Outputs CSS classes to assist in styling comments.

This template tag is primarily intended for template authors.

The function outputs a string: class="classes", where instead of "classes" there can be values: comment, even (or odd), thread-even, depth-1 etc., depending on which comment is currently being displayed. This allows for easy styling of comments in different ways.

Typically, the function is used like this:

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

Under what conditions certain classes are applied:

  • comment_type ― for regular comments adds the class comment. For all other types, the comment type comment_type is added as a class.

  • byuser comment-author-USER_NICENAME ― if the comment was made by a registered user, the class byuser and comment-author-USER_NICENAME is added (i.e., spaces are removed).

  • bypostauthor ― if the comment is written by the post author, the class bypostauthor is added.

  • even/alt odd ― if the comment number is even, the class even is added. Otherwise, the class alt and odd are added.

  • depth-1 ― Always adds the class depth-COMMENT_DEPTH.

  • thread-even|thread-odd ― If the depth of the top-level comment (1), then thread-even or thread-alt thread-odd is added depending on whether the comment is even or odd.

This function uses global variables that can be changed in advance to affect the result of the function:

$comment_alt
$comment_depth
$comment_thread_alt

For example, you can forcibly cancel the alt class for the first comment (the class even will be output): $comment_alt=false;.

1 time — 0.001315 sec (very slow) | 50000 times — 2.64 sec (fast) | PHP 7.0.5, WP 4.4.2

No Hooks.

Returns

null|String. Comment classes when echo = false. Nothing when echo = true.

Usage

<li <?php comment_class( $class, $comment, $post_id, $echo ); ?>>
$class(string)
Your own custom class to add to the other classes.
Default: ''
$comment(int/object)
ID or object of the comment for which to output classes.
Default: null
$post_id(int/object)
ID or object of the post associated with the comment for which to output classes.
Default: null
$echo(boolean)
Output the result to the screen (true) or return for processing (false).
Default: true

Examples

0

#1 Suppose the function is called for an even top-level comment:

<?php comment_class(); ?>

Display: class="comment even thread-even"

In reality, the function is used inside the HTML tag (comment container):

<li <?php comment_class( $class, $comment, $post_id, $echo ); ?> id="li-comment-<?php comment_ID() ?>">
0

#2 Add my special class next to the others:

For special cases where you want to add your own classes, you can use first parameter:

<?php comment_class('special'); ?>

We get: class="comment even thread-even special".

Changelog

Since 2.7.0 Introduced.
Since 4.4.0 Added the ability for $comment to also accept a WP_Comment object.

comment_class() code WP 6.9

function comment_class( $css_class = '', $comment = null, $post = null, $display = true ) {
	// Separates classes with a single space, collates classes for comment DIV.
	$css_class = 'class="' . implode( ' ', get_comment_class( $css_class, $comment, $post ) ) . '"';

	if ( $display ) {
		echo $css_class;
	} else {
		return $css_class;
	}
}