comment_class()
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 classcomment. For all other types, the comment typecomment_typeis added as a class. -
byuser comment-author-USER_NICENAME― if the comment was made by a registered user, the classbyuserandcomment-author-USER_NICENAMEis added (i.e., spaces are removed). -
bypostauthor― if the comment is written by the post author, the classbypostauthoris added. -
even/alt odd― if the comment number is even, the classevenis added. Otherwise, the classaltandoddare added. -
depth-1― Always adds the classdepth-COMMENT_DEPTH. thread-even|thread-odd― If the depth of the top-level comment (1), thenthread-evenorthread-alt thread-oddis 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;.
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
#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() ?>">
#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() 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;
}
}