get_comment_class

Returns the classes for the comment div as an array.

Description

(array) get_comment_class( (string) $class = '', (constant) $comment_id = null, (null) $post_id = null ); 

Returns (array)

An array of classes.

Parameters (3)

0. $class — Optional. (string) => ''
One or more classes to add to the class list. Default empty.
1. $comment_id — Optional. (constant) => null
Comment ID or WP_Comment object. Default current comment.
2. $post_id — Optional. (null) => null
Post ID or WP_Post object. Default current post.

Usage

  1. if ( !function_exists( 'get_comment_class' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment-template.php'; 
  3.  
  4. // Optional. One or more classes to add to the class list. Default empty. 
  5. $class = ''; 
  6.  
  7. // Comment ID or WP_Comment object. Default current comment. 
  8. $comment_id = null; 
  9.  
  10. // Post ID or WP_Post object. Default current post. 
  11. $post_id = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = get_comment_class($class, $comment_id, $post_id); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/comment-template.php  
  1. function get_comment_class( $class = '', $comment_id = null, $post_id = null ) { 
  2. global $comment_alt, $comment_depth, $comment_thread_alt; 
  3.  
  4. $classes = array(); 
  5.  
  6. $comment = get_comment( $comment_id ); 
  7. if ( ! $comment ) { 
  8. return $classes; 
  9.  
  10. // Get the comment type (comment, trackback),  
  11. $classes[] = ( empty( $comment->comment_type ) ) ? 'comment' : $comment->comment_type; 
  12.  
  13. // Add classes for comment authors that are registered users. 
  14. if ( $comment->user_id > 0 && $user = get_userdata( $comment->user_id ) ) { 
  15. $classes[] = 'byuser'; 
  16. $classes[] = 'comment-author-' . sanitize_html_class( $user->user_nicename, $comment->user_id ); 
  17. // For comment authors who are the author of the post 
  18. if ( $post = get_post($post_id) ) { 
  19. if ( $comment->user_id === $post->post_author ) { 
  20. $classes[] = 'bypostauthor'; 
  21.  
  22. if ( empty($comment_alt) ) 
  23. $comment_alt = 0; 
  24. if ( empty($comment_depth) ) 
  25. $comment_depth = 1; 
  26. if ( empty($comment_thread_alt) ) 
  27. $comment_thread_alt = 0; 
  28.  
  29. if ( $comment_alt % 2 ) { 
  30. $classes[] = 'odd'; 
  31. $classes[] = 'alt'; 
  32. } else { 
  33. $classes[] = 'even'; 
  34.  
  35. $comment_alt++; 
  36.  
  37. // Alt for top-level comments 
  38. if ( 1 == $comment_depth ) { 
  39. if ( $comment_thread_alt % 2 ) { 
  40. $classes[] = 'thread-odd'; 
  41. $classes[] = 'thread-alt'; 
  42. } else { 
  43. $classes[] = 'thread-even'; 
  44. $comment_thread_alt++; 
  45.  
  46. $classes[] = "depth-$comment_depth"; 
  47.  
  48. if ( !empty($class) ) { 
  49. if ( !is_array( $class ) ) 
  50. $class = preg_split('#\s+#', $class); 
  51. $classes = array_merge($classes, $class); 
  52.  
  53. $classes = array_map('esc_attr', $classes); 
  54.  
  55. /** 
  56. * Filters the returned CSS classes for the current comment. 
  57. * @since 2.7.0 
  58. * @param array $classes An array of comment classes. 
  59. * @param string $class A comma-separated list of additional classes added to the list. 
  60. * @param int $comment_id The comment id. 
  61. * @param WP_Comment $comment The comment object. 
  62. * @param int|WP_Post $post_id The post ID or WP_Post object. 
  63. */ 
  64. return apply_filters( 'comment_class', $classes, $class, $comment->comment_ID, $comment, $post_id );