返回介绍

get_comment_class()

发布于 2017-09-10 23:05:31 字数 4186 浏览 956 评论 0 收藏 0

get_comment_class( string|array $class = '',  int|WP_Comment $comment_id = null,  int|WP_Post $post_id = null )

Returns the classes for the comment div as an array.


description


参数

$class

(string|array) (Optional) One or more classes to add to the class list.

Default value: ''

$comment_id

(int|WP_Comment) (Optional) Comment ID or WP_Comment object. Default current comment.

Default value: null

$post_id

(int|WP_Post) (Optional) Post ID or WP_Post object. Default current post.

Default value: null


返回值

(array) An array of classes.


源代码

File: wp-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.
	if ( $comment->user_id > 0 && $user = get_userdata( $comment->user_id ) ) {
		$classes[] = 'byuser';
		$classes[] = 'comment-author-' . sanitize_html_class( $user->user_nicename, $comment->user_id );
		// For comment authors who are the author of the post
		if ( $post = get_post($post_id) ) {
			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 array       $classes    An array of comment classes.
	 * @param string      $class      A comma-separated list of additional classes added to the list.
	 * @param int         $comment_id The comment id.
	 * @param WP_Comment  $comment    The comment object.
	 * @param int|WP_Post $post_id    The post ID or WP_Post object.
	 */
	return apply_filters( 'comment_class', $classes, $class, $comment->comment_ID, $comment, $post_id );
}

更新日志

Versiondescription
4.4.0Added the ability for $comment_id to also accept a WP_Comment object.
2.7.0Introduced.

相关函数

Uses

  • wp-includes/formatting.php: sanitize_html_class()
  • wp-includes/pluggable.php: get_userdata()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/post.php: get_post()
  • wp-includes/comment-template.php: comment_class
  • wp-includes/comment.php: get_comment()
  • Show 1 more use Hide more uses

Used By

  • wp-admin/includes/class-wp-comments-list-table.php: WP_Comments_List_Table::single_row()
  • wp-includes/comment-template.php: comment_class()

User Contributed Notes

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文