Using WordPress ‘comment_ID()’ PHP function

The comment_ID() WordPress PHP function is used to display the comment ID of the current comment.

Usage

Here’s a generic example on how to use comment_ID() function:

<div id="comment-comment_ID()">Comment by comment_author(): </div> 
<div class="comment-text">comment_text()</div>

In the above example, comment_ID() is used to uniquely identify a comment within the div element.

Parameters

  • There are no parameters for the comment_ID() function.

More information

See WordPress Developer Resources: comment_ID()

Examples

Displaying Comment ID

In this example, comment_ID() is used to display the comment ID.

<div id="comment-comment_ID()">
  This is comment number comment_ID()
</div>

Here, the comment_ID() function is used in the creation of a reply link.

<a href="replytocom=comment_ID()">Reply to this comment</a>

Using Comment ID for CSS Styling

This example uses comment_ID() to apply CSS styling to a specific comment.

<div id="comment-comment_ID()" class="highlighted-comment">
  Highlighted comment: comment_ID()
</div>

Using Comment ID in a Loop

In this example, comment_ID() is used inside a loop to display multiple comments.

while (have_comments()) : the_comment();
  <div id="comment-comment_ID()">
    Comment number: comment_ID()
  </div>
endwhile;

Nesting Comment ID

Here, comment_ID() is used in a nested div structure.

<div id="parent-comment_ID()">
  <div id="child-comment_ID()">
    Child comment of parent comment comment_ID()
  </div>
</div>

In all the examples, the comment_ID() function is used to display the ID of the comment in various ways such as in an id attribute, reply link, CSS class, loop, and nested structure.