^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 5年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云1核2g仅需102元/年,今日10点开抢

wordpress函数comment_class()用法示例

comment_class( string|array $class = '',  int|WP_Comment $comment = null,  int|WP_Post $post_id = null,  bool $echo = true )

Generates semantic classes for each comment element.


描述


参数

$class

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

Default value: ”

$comment

(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

$echo

(bool)
(Optional)
Whether to cho or return the output.

Default value: true


返回值

(string) If $echo is false, the class will be returned. Void otherwise.


源代码

File: wp-includes/comment-template.php

function comment_class( $class = '', $comment = 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, $post_id ) ) . '"';
	if ( $echo)
		echo $class;
	else
		return $class;
}

更新日志

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

相关函数

Uses

  • wp-includes/comment-template.php:
    get_comment_class()

Used By

  • wp-admin/includes/dashboard.php:
    _wp_dashboard_recent_comments_row()
  • wp-includes/class-walker-comment.php:
    Walker_Comment::comment()
  • wp-includes/class-walker-comment.php:
    Walker_Comment::html5_comment()
  • wp-includes/class-walker-comment.php:
    Walker_Comment::ping()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Example

    
    <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:

    
    <?php comment_class( 'special' ); ?>
    

    This will add “special” to the class list.

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈