函数文档

comment_class()

💡 云策文档标注

概述

comment_class() 函数用于为评论元素生成语义化的 CSS 类,便于主题开发者进行样式定制。它基于评论类型、用户身份、奇偶性、深度等条件自动添加类,并支持自定义类参数。

关键要点

  • 函数参数包括 $css_class(可选自定义类)、$comment(评论 ID 或 WP_Comment 对象)、$post(文章 ID 或 WP_Post 对象)和 $display(控制输出方式)。
  • 自动生成的类包括:comment_type 相关类(如 "comment")、user_id 相关类(如 "byuser"、"bypostauthor")、奇偶类("even" 或 "odd"、"alt")、深度类(如 "depth-1")和顶级评论类(如 "thread-even")。
  • 通过全局变量 $comment_alt、$comment_depth、$comment_thread_alt 可影响输出,例如强制设置起始评论为偶数。
  • 函数返回 void 或字符串,取决于 $display 参数;若为 true 则直接输出,否则返回类字符串。
  • 相关函数包括 get_comment_class(),用于以数组形式返回类列表。

代码示例

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

注意事项

  • 在调用 comment_class() 前,可通过设置全局变量来调整类生成逻辑,例如 $comment_alt = FALSE 以使首评论为偶数。
  • 自定义类参数 $css_class 可以是字符串或数组,将添加到所有自动生成的类中。
  • 函数自 WordPress 2.7.0 引入,4.4.0 版本增强了 $comment 参数以支持 WP_Comment 对象。

📄 原文内容

Generates semantic classes for each comment element.

Parameters

$css_classstring|string[]optional
One or more classes to add to the class list.
Default empty.
$commentint|WP_Commentoptional
Comment ID or WP_Comment object. Default current comment.

Default:null

$postint|WP_Postoptional
Post ID or WP_Post object. Default current post.

Default:null

$displaybooloptional
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 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.

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

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

Changelog

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

User Contributed Notes

  1. Skip to note 2 content

    Example

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

    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 make it easy to style different parts of the theme in different ways.

    Specifically, it 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.

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

    This will add “special” to the class list.