Function and usage of comment_text() in wordpress


Answers ( 1 )


    The comment_text() function in WordPress is used to display the text of the current comment in a theme template. Here's a detailed explanation of its function and usage:


    comment_text( int|WP_Comment $comment_id, array $args = array() )


    • comment_text() is a WordPress template tag that outputs the text of a comment.
    • It's often used within the WordPress Loop or a comment loop to display the content of each comment.
    • This function is a part of WordPress’s comment template tags, which are used to display comments and comment-related information on a post.


    1. $comment_id (int|WP_Comment, optional):

      • This parameter can be either an integer (the ID of the comment) or a WP_Comment object.
      • If this parameter is not provided, the function will default to the current comment in the loop.
    2. $args (array, optional):

      • An associative array of arguments that can modify the function's output.
      • By default, it is an empty array.
      • These arguments are generally passed to control the markup or behavior of the comment text display.

    Related Function:

    • Walker_Comment::comment():
      • Walker_Comment::comment() is a method of the Walker_Comment class.
      • It is responsible for displaying a single comment by walking through each comment provided to it.
      • The comment_text() function is often used inside this method to render the text of each comment.

    Sample Usage:

    Here's an example of how comment_text() might be used in a WordPress theme:

    // Loop through each comment
    foreach ( $comments as $comment ) {
        // Set the global comment variable
        $GLOBALS['comment'] = $comment;
        // Display the comment text

    In this example, the function loops through each comment and displays its text. This is a simplified version of what might be found in a WordPress theme's comments template.

    Remember, comment_text() should be used within the context of a comment loop, and it relies on global variables set by WordPress during this loop. This makes it specific to the WordPress templating system and is primarily used for theme development.

Leave an answer