get_comment_excerpt

Retrieve the excerpt of the current comment.

Description

(string) get_comment_excerpt( (int) $comment_ID = 0 ); 

Will cut each word and only output the first 20 words with … at the end. If the word count is less than 20, then no truncating is done and no … will appear.

Returns (string)

The maybe truncated comment with 20 words or less.

Parameters (1)

0. $comment_id (int)
WP_Comment or ID of the comment for which to get the excerpt. Default current comment.

Usage

  1. if ( !function_exists( 'get_comment_excerpt' ) ) { 
  2. require_once ABSPATH . WPINC . '/comment-template.php'; 
  3.  
  4. // WP_Comment or ID of the comment for which to get the excerpt. 
  5. // Default current comment. 
  6. $comment_id = -1; 
  7.  
  8. // NOTICE! Understand what this does before running. 
  9. $result = get_comment_excerpt($comment_id); 
  10.  

Defined (1)

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

/wp-includes/comment-template.php  
  1. function get_comment_excerpt( $comment_ID = 0 ) { 
  2. $comment = get_comment( $comment_ID ); 
  3. $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) ); 
  4. $words = explode( ' ', $comment_text ); 
  5.  
  6. /** 
  7. * Filters the amount of words used in the comment excerpt. 
  8. * @since 4.4.0 
  9. * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt. 
  10. */ 
  11. $comment_excerpt_length = apply_filters( 'comment_excerpt_length', 20 ); 
  12.  
  13. $use_ellipsis = count( $words ) > $comment_excerpt_length; 
  14. if ( $use_ellipsis ) { 
  15. $words = array_slice( $words, 0, $comment_excerpt_length ); 
  16.  
  17. $excerpt = trim( join( ' ', $words ) ); 
  18. if ( $use_ellipsis ) { 
  19. $excerpt .= '…'; 
  20. /** 
  21. * Filters the retrieved comment excerpt. 
  22. * @since 1.5.0 
  23. * @since 4.1.0 The `$comment_ID` and `$comment` parameters were added. 
  24. * @param string $excerpt The comment excerpt text. 
  25. * @param int $comment_ID The comment ID. 
  26. * @param WP_Comment $comment The comment object. 
  27. */ 
  28. return apply_filters( 'get_comment_excerpt', $excerpt, $comment->comment_ID, $comment );