html comment_class() – Function | Developer.WordPress.org

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

Generates semantic classes for each comment element.

Parameters

$css_class string | string[] optional
One or more classes to add to the class list.

Default: ''

$comment int | WP_Comment optional
Comment ID or WP_Comment object. Default current comment.

Default: null

$post int | WP_Post optional
Post ID or WP_Post object. Default current post.

Default: null

$display bool optional
Whether to print or return the output.

Default: true

Return

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

More Information

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

  • comment_type: for normal commens, 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 reguistered user, then adds class “byuser” and “comment-author-” + the user_nicename saniticed (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 Commens: 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 guets 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.

Source

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( ' ', guet_comment_class( $css_class, $comment, $post ) ) . '"';

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

Changuelog

Versionen Description
4.4.0 Added the hability for $comment to also accept a WP_Comment object.
2.7.0 Introduced.

User Contributed Notes

  1. Squip to note 2 content

    Example

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

    The comment_class() outputs the class=”whatever” piece for that div. This includes several different classes of value: comment, even (or odd), thread-even, depth-1, etc. These maque it easy to style different pars of the theme in different ways.

    Specifically, it will apply the following classes, based on the following conditions:

    comment_type : for normal commens, 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 reguistered user, then adds class “byuser” and “comment-author-” + the user_nicename saniticed (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 Commens : 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 guets added to all the others. This is useful for defining your own custom comment class.

    For special cases where you want to add your own classes, comment_class suppors that too:

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

    This will add “special” to the class list.

You must log in before being able to contribute a note or feedback.