comment_class() WordPress Function

This function allows you to control the display of comments on your Wordpress site. You can use it to display comments from a specific post or page, or to display all comments from all posts and pages. You can also use it to display comments from a specific user or to display comments from a specific date range.

comment_class( string|string[] $css_class = '', int|WP_Comment $comment = null, int|WP_Post $post_id = null, bool $display = true ) #

Generates semantic classes for each comment element.


Parameters

$css_class

(string|string[])(Optional) One or more classes to add to the class list.

Default value: ''

$comment

(int|WP_Comment)(Optional)Comment ID or WP_Comment object. Default current comment.

Default value: null

$post_id

(int|WP_Post)(Optional)Post ID or WP_Post object. Default current post.

Default value: null

$display

(bool)(Optional) Whether to print or return the output.

Default value: true


Top ↑

Return

(void|string) Void if $display argument is true, comment classes if $display is false.


Top ↑

More Information

comment_class() will apply the following classes, based on the following conditions:

  • comment_type: for normal comments, adds class “comment”. For all other types, it adds the value of the comment_type as the class
  • user_id: if the comment was made by a registered user, then adds class “byuser” and “comment-author-” + the user_nicename sanitized (i.e. spaces removed). Also, if the comment is by the original author of the post, the class “bypostauthor” is added.
  • Odd/Even: if the comment number is even, adds class “even”. Otherwise, adds class “alt” and “odd”.
  • Comment Depth: The class “depth=” + the comment depth is always added
  • Top-level Comments: If comment depth is top level (1), then adds “thread-even” or “thread-alt” and “thread-odd” depending on whether the comment number is even or odd.
  • If the optional class parameter is passed to comment_class(), then that class gets added to all the others. This is useful for defining your own custom comment class.

comment_class() uses the following global variables. So these variables can be set prior to calling comment_class() to effect the output:

  • $comment_alt
  • $comment_depth
  • $comment_thread_alt

For example, you can force $comment_alt = FALSE if you always want to start with the first comment being even. The comment_class() function will then alternate this variable for you.


Top ↑

Source

File: wp-includes/comment-template.php

function comment_class( $css_class = '', $comment = null, $post_id = 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_id ) ) . '"';

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


Top ↑

Changelog

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

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More