get_comment_class()
最后更新于:2021-11-26 08:35:01
get_comment_class( string|string[]$class=”, int|GC_Comment$comment_id=null, int|GC_Post$post_id=null)响应s the classes for the comment div as an array.
参数
- $class
-
(string|string[]) (Optional) One or more classes to add to the class list.
Default value: ”
- $comment_id
-
(int|GC_Comment) (Optional) Comment ID or GC_Comment object. Default current comment.
Default value: null
- $post_id
-
(int|GC_Post) (Optional) Post ID or GC_Post object. Default current post.
Default value: null
响应
(string[]) An array of classes.
源文件
文件: gc-includes/comment-template.php
function get_comment_class( $class = '', $comment_id = null, $post_id = null ) {
global $comment_alt, $comment_depth, $comment_thread_alt;
$classes = array();
$comment = get_comment( $comment_id );
if ( ! $comment ) {
return $classes;
}
// Get the comment type (comment, trackback).
$classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type;
// Add classes for comment authors that are registered users.
$user = $comment->user_id ? get_userdata( $comment->user_id ) : false;
if ( $user ) {
$classes[] = 'byuser';
$classes[] = 'comment-author-' . sanitize_html_class( $user->user_nicename, $comment->user_id );
// For comment authors who are the author of the post.
$post = get_post( $post_id );
if ( $post ) {
if ( $comment->user_id === $post->post_author ) {
$classes[] = 'bypostauthor';
}
}
}
if ( empty( $comment_alt ) ) {
$comment_alt = 0;
}
if ( empty( $comment_depth ) ) {
$comment_depth = 1;
}
if ( empty( $comment_thread_alt ) ) {
$comment_thread_alt = 0;
}
if ( $comment_alt % 2 ) {
$classes[] = 'odd';
$classes[] = 'alt';
} else {
$classes[] = 'even';
}
$comment_alt++;
// Alt for top-level comments.
if ( 1 == $comment_depth ) {
if ( $comment_thread_alt % 2 ) {
$classes[] = 'thread-odd';
$classes[] = 'thread-alt';
} else {
$classes[] = 'thread-even';
}
$comment_thread_alt++;
}
$classes[] = "depth-$comment_depth";
if ( ! empty( $class ) ) {
if ( ! is_array( $class ) ) {
$class = preg_split( '#s+#', $class );
}
$classes = array_merge( $classes, $class );
}
$classes = array_map( 'esc_attr', $classes );
/**
* Filters the returned CSS classes for the current comment.
*
* @since 2.7.0
*
* @param string[] $classes An array of comment classes.
* @param string[] $class An array of additional classes added to the list.
* @param int $comment_id The comment ID.
* @param GC_Comment $comment The comment object.
* @param int|GC_Post $post_id The post ID or GC_Post object.
*/
return apply_filters( 'comment_class', $classes, $class, $comment->comment_ID, $comment, $post_id );
}