WordPress comment_excerpt() Function

Beginner
4 min read
Updated: Nov 2026
3 examples
Template tag

Definition

In WordPress, comment_excerpt() prints a short version of a comment. It is useful for recent-comments lists.

WordPress removes HTML tags and keeps the first 20 words. If the comment is longer, it adds an ellipsis.

Syntax

Call it inside the comment loop, or pass a comment ID.

WordPress
comment_excerpt();
comment_excerpt( 15 );
CallWhat it does
comment_excerpt()Prints the excerpt of the current comment.
comment_excerpt( 15 )Prints the excerpt of comment 15.
get_comment_excerpt()Returns the excerpt instead of printing it.

You can change the word count with the comment_excerpt_length filter.

Full declaration
WordPress
comment_excerpt( int|WP_Comment $comment_id = 0 ): void

Parameters

ParameterDescription
$comment_idOptional. A comment ID or a WP_Comment object. Default 0, which means the current comment.

Return Value

Return ValueDescription
voidNothing is returned. The function prints the excerpt.

WordPress Examples

Three short theme examples. Each one has a sample output.

Example 1 — Basic use

Print the excerpt in a list.

WordPress
<li><?php comment_author(); ?>: <?php comment_excerpt(); ?></li>

How It Works

  1. The comment is cut after 20 words.
  2. WordPress adds an ellipsis when it is cut.
  3. Tags are removed, so the output is plain text.

Example 2 — By comment ID

Print the excerpt of one comment.

WordPress
<p><?php comment_excerpt( 15 ); ?></p>

How It Works

  1. The argument 15 is the comment ID.
  2. A short comment is printed in full.
  3. No ellipsis is added when the comment is short.

Example 3 — Change the length

Make every excerpt 10 words long.

WordPress
add_filter( 'comment_excerpt_length', function () {
  return 10;
} );

How It Works

  1. The filter comment_excerpt_length sets the word count.
  2. Put this code in your theme's functions.php.
  3. comment_excerpt() now stops after 10 words.

Frequently Asked Questions

It prints a short plain-text version of the current comment.
By default 20 words.
Use the comment_excerpt_length filter.
No. Tags are removed.
Use comment_text().

Next: comment_form()

Print the complete comment form with fields and a submit button.

comment_form() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful