The comment_text() WordPress PHP function is used to display the text of the current comment.
Usage
To use the function, simply call comment_text()
within the loop of your WordPress theme file. You can also pass a comment ID or a WP_Comment
object to this function to get text from a specific comment. Here is a custom example:
$comment_id = 10; // Assume this is a valid comment ID comment_text($comment_id);
This will display the text for the comment with an ID of 10.
Parameters
- $comment_id (int|WP_Comment – Optional): The ID of the comment or a
WP_Comment
object for which to print the text. The default is the current comment. - $args (array – Optional): An array of arguments. Default is an empty array.
More information
See WordPress Developer Resources: comment_text()
This function is part of the WordPress core and is used throughout various WordPress themes and plugins. It has not been deprecated and is maintained with the latest versions of WordPress.
Examples
Displaying a Comment’s Text
This example demonstrates how to display the text of a specific comment using its ID.
$comment_id = 5; // This should be a valid comment ID comment_text($comment_id);
Use within The Loop
This example shows how to use comment_text()
inside the WordPress loop, to display the text of each comment in a post.
while (have_comments()) { the_comment(); comment_text(); }
Displaying Comment Text with Default Text
In this example, if the comment text is empty, a default text will be displayed.
$comment_id = 7; // This should be a valid comment ID $text = comment_text($comment_id); if (empty($text)) { echo 'No comment text available'; }
Using comment_text()
with a WP_Comment
Object
This example shows how to use comment_text()
with a WP_Comment
object.
$comment = get_comment(8); // Assume this returns a valid WP_Comment object comment_text($comment);
Using comment_text()
with an Array of Arguments
This example demonstrates how to use comment_text()
with an array of arguments. Note: As of the date of this guide, this function doesn’t actually use the $args
parameter. This is for future reference.
$comment_id = 10; // This should be a valid comment ID $args = array( 'foo' => 'bar', ); comment_text($comment_id, $args);