comment_class()WP 2.7.0

Generates semantic classes for each comment element.

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

No Hooks.

Return

null|String. Void if $display argument is true, comment classes if $display is false.

Usage

comment_class( $css_class, $comment, $post, $display );
$css_class(string|string[])
One or more classes to add to the class list.
Default: ''
$comment(int|WP_Comment)
Comment ID or WP_Comment object.
Default: current comment
$post(int|WP_Post)
Post ID or WP_Post object.
Default: current post
$display(true|false)
Whether to print or return the output.
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.5.3

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;
	}
}