WordPress函数文档comment_class()
动态显示不同评论的class 描述 This function displays comment classe…
动态显示不同评论的class
描述
This function displays comment classes, which will help theme authors perform simpler styling.
See also post_class() for more details.
用法
Default usage:<?php comment_class(); ?>
With all parameters:<?php comment_class( $class, $comment_id, $post_id, $echo ) ?>
参数
$class
(string/array) (可选) One or more classes to add to the class list
默认值: Empty string
$comment_id
(integer) (可选) A comment ID
默认值: null
$post_id
(integer) (可选) A post ID
默认值: null
$echo
(boolean) (可选) Whether comment_class should echo or return
默认值: true
示例
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<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 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:
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?php comment_class(‘special’); ?>
|
This will add “special” to the class list.
注意
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.
历史
- 添加于 版本 2.7.0
源文件
comment_class() 函数的代码位于 wp-includes/comment-template.php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* 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;
}
|
- 原文:http://codex.wordpress.org/Function_Reference/comment_class
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!