动态显示不同评论的class
描述
此函数显示注释类,这将有助于主题作者执行更简单的样式设置。
有关更多详细信息,另请参阅 post_class()。
用法
默认用法:
<?php comment_class(); ?>
使用所有参数:
<?php comment_class( $class, $comment_id, $post_id, $echo ) ?>
参数
$class
(string/array) (可选) 要添加到类列表中的一个或多个类
默认值: Empty string
$comment_id
(integer) (可选)注释 ID
默认值: null
$post_id
(integer) (可选) 帖子编号 ID
默认值: null
$echo
(boolean) (可选) comment_class应该回声还是返回
默认值: true
示例
<li <?php comment_class(); ?> id=“li-comment-<?php comment_ID() ?>“>
comment_class() 输出该 div 的 class=“whatever” 片段。这包括几种不同的值类:注释、偶数(或奇数)、线程偶数、深度 1 等。这些可以轻松地以不同的方式设置主题的不同部分的样式。
具体而言,它将根据以下条件应用以下类:
comment_type:对于普通注释,添加类“注释”。对于所有其他类型,它将comment_type的值添加为类
user_id:如果评论是由注册用户发表的,则添加类“byuser”和“comment-author-”+经过净化的user_nicename(即删除空格)。此外,如果评论是由帖子的原始作者撰写的,则会添加“bypostauthor”类。
Odd/Even: 如果注释号为偶数,则添加类“偶数”。否则,添加类“alt”和“odd”。
Comment Depth: 始终添加类“depth=”+注释深度
Top-level Comments: 如果注释深度为顶级 (1),则根据注释编号是偶数还是奇数,添加“线程偶数”或“线程替代”和“线程奇数”。
如果将可选的类参数传递给 comment_class(),则该类将添加到所有其他类中。这对于定义您自己的自定义注释类非常有用。
对于想要添加自己的类的特殊情况,comment_class也支持:
<?php comment_class(‘special’); ?>
这会将“特殊”添加到类列表中。
注意
comment_class() 使用以下全局变量。因此,可以在调用 comment_class() 之前设置这些变量以影响输出:
$comment_alt
$comment_depth
$comment_thread_alt
例如,如果您始终希望从第一个注释为偶数开始,则可以强制 $comment_alt = FALSE。然后,comment_class() 函数将为您替换此变量。
历史
添加于 版本 2.7.0
源文件
comment_class() 函数的代码位于 wp-includes/comment-template.php
.
/**
* Generates semantic classes for each comment element.
*
* @since 2.7.0
*
* @param string|array $class Optional. One or more classes to add to the class list.
* Default empty.
* @param int $comment_id Comment ID. Default current comment.
* @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
* @param bool $echo Optional. Whether to cho or return the output.
* Default true.
* @return string|void
*/
function comment_class( $class = ”, $comment_id = null, $post_id = null, $echo = true ) {
// Separates classes with a single space, collates classes for comment DIV
$class = ‘class=”‘ . join( ‘ ‘, get_comment_class( $class, $comment_id, $post_id ) ) . ‘”‘;
if ( $echo)
echo $class;
else
return $class;
}