The comment_excerpt() WordPress PHP function displays the excerpt of the current comment in a WordPress blog or post. It can be used to show a short preview of a comment.
Usage
To use the comment_excerpt() function, you can simply call it without any parameters to display the excerpt of the current comment. If you want to display the excerpt of a specific comment, you can pass the comment ID or WP_Comment object as a parameter.
For example:
Latest comment: comment_excerpt(123);
This will display the excerpt of the comment with ID 123.
Parameters
- $comment_id (int|WP_Comment, Optional): The ID of the comment or the WP_Comment object for which to print the excerpt. By default, it prints the excerpt for the current comment.
More information
See WordPress Developer Resources: comment_excerpt()
This function was introduced in WordPress version 0.71. It is not deprecated and you can find its source code in wp-includes/comment-template.php.
Examples
Display the excerpt of the current comment
<!-- Display the excerpt of the current comment --> comment_excerpt();
This will print out the excerpt of the current comment in the loop.
Display the excerpt of a specific comment by ID
<!-- Display the excerpt of a specific comment by ID --> comment_excerpt(123);
This will print out the excerpt of the comment with the ID 123.
Display the excerpt of a specific comment using WP_Comment object
<!-- Get a comment object --> $comment_obj = get_comment(123); <!-- Display the excerpt of a specific comment using WP_Comment object --> comment_excerpt($comment_obj);
This will print out the excerpt of the comment that is represented by the WP_Comment object.
Use within a comments loop
<!-- Start the comments loop --> while (have_comments()) { the_comment(); <!-- Display the excerpt of each comment --> comment_excerpt(); }
This will print out the excerpt of each comment in the comments loop.
Display with a custom message
<!-- Display the excerpt of a specific comment with a custom message --> echo 'Latest comment: ' . comment_excerpt(123);
This will print out a custom message along with the excerpt of the comment with the ID 123.