
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.
comment_excerpt();
comment_excerpt( 15 );
| Call | What 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
comment_excerpt( int|WP_Comment $comment_id = 0 ): void
Parameters
| Parameter | Description |
|---|
$comment_id | Optional. A comment ID or a WP_Comment object. Default 0, which means the current comment. |
Return Value
| Return Value | Description |
|---|
| void | Nothing 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.
<li><?php comment_author(); ?>: <?php comment_excerpt(); ?></li>
<li>Jane Doe: Thanks for this guide! I followed every step and my comment form now works…</li>
How It Works
- The comment is cut after 20 words.
- WordPress adds an ellipsis when it is cut.
- Tags are removed, so the output is plain text.
Example 2 — By comment ID
Print the excerpt of one comment.
<p><?php comment_excerpt( 15 ); ?></p>
<p>Great post, thank you!</p>
How It Works
- The argument
15 is the comment ID. - A short comment is printed in full.
- No ellipsis is added when the comment is short.
Example 3 — Change the length
Make every excerpt 10 words long.
add_filter( 'comment_excerpt_length', function () {
return 10;
} );
Thanks for this guide! I followed every step and my…
How It Works
- The filter
comment_excerpt_length sets the word count. - Put this code in your theme's functions.php.
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().
About the author
Developer, cloud engineer, and technical writer
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