Using comment_class to add CSS classes to WordPress comments

The comment_class function in WordPress is designed to generate CSS class attributes for comment elements in a page. It is used to add different classes to the comments, allowing for easier styling and manipulation through CSS. This function automatically adds several classes including the comment’s status, the comment author’s name, and whether the current user is the author of the comment.

Being able to add classes to comments can be beneficial for a variety of reasons. For instance, it allows for detailed customization of the comment’s appearance. It also helps to highlight certain comments, such as those from the post’s author or from registered users. Furthermore, it can aid in distinguishing between different types of comments, like trackbacks or pingbacks.

While the comment_class function can be used without any parameters to automatically generate a set of classes, it also accepts additional parameters to add custom classes. This provides a greater level of control over the styling of the comments.

Parameters Accepted by the WordPress comment_class Function

The comment_class function in WordPress accepts four parameters. These parameters are:

  • $css_class (string|string[]): This is an optional parameter. Its default value is an empty string (”). It is used to add one or more classes to the class list.
  • $comment (int|WP_Comment): This is also an optional parameter. Its default value is null. It represents the Comment ID or WP_Comment object. By default, it is the current comment.
  • $post (int|WP_Post): This optional parameter has a default value of null. It represents the Post ID or WP_Post object. By default, it is the current post.
  • $display (bool): This is an optional parameter as well, with a default value of true. It determines whether to print or return the output.

Return Value of the WordPress comment_class Function

The comment_class function in WordPress returns either void or a string. If the $display argument is set to true, the function returns void. However, if $display is set to false, the function returns the comment classes.

If the function does not accept any parameters, it will be explicitly stated. However, in this case, the comment_class function does accept parameters.

Examples

How to use comment_class function in WordPress

The comment_class function in WordPress is primarily used for styling comments differently based on their properties. Here’s a basic example:

<?php
foreach ($comments as $comment) {
 echo '<div ';
 comment_class('', $comment->comment_ID, $comment->comment_post_ID);
 echo '>';
 echo get_comment_text($comment);
 echo '</div>';
}
?>

In this code snippet, we’re looping through a list of comments. For each comment, we’re creating a new div element and using the comment_class function to add classes to it. These classes are based on the properties of the comment, such as whether it’s by an admin, a registered user, etc. This allows us to style these comments differently in our CSS.

How to add custom class to comment_class function in WordPress

You can also add your own custom classes to the comment_class function. Here’s how:

<?php
foreach ($comments as $comment) {
 echo '<div ';
 comment_class('my-custom-class', $comment->comment_ID, $comment->comment_post_ID);
 echo '>';
 echo get_comment_text($comment);
 echo '</div>';
}
?>

In this example, we’re adding a custom class called ‘my-custom-class’ to each comment. This can be useful if you want to style certain comments in a specific way that isn’t covered by the default classes.

How to use comment_class function with if condition in WordPress

You can use the comment_class function in combination with an if condition to add classes based on certain conditions. Here’s an example:

<?php
foreach ($comments as $comment) {
 echo '<div ';
 if ($comment->comment_approved == '0') {
 comment_class('unapproved', $comment->comment_ID, $comment->comment_post_ID);
 } else {
 comment_class('', $comment->comment_ID, $comment->comment_post_ID);
 }
 echo '>';
 echo get_comment_text($comment);
 echo '</div>';
}
?>

In this case, we’re adding the ‘unapproved’ class to any comments that haven’t been approved yet. This could be useful for visually distinguishing unapproved comments in the comments list.

Conclusion

The comment_class function in WordPress is a tool that provides a way to generate a list of classes for each comment. This function is primarily used to style and format the comments section on a WordPress site. It provides a dynamic way to assign classes to comments, which can be used to apply different styles to different types of comments. This function is a part of the WordPress core and is used in the comments loop, which is the part of the WordPress template that displays comments on a post or page.

Related WordPress Functions