Example 1 — Make an id
Give each comment a unique id.
<li id="comment-<?php comment_ID(); ?>"> How It Works
comment_ID()prints the number.- The word
comment-in front makes a valid id. - Each comment gets a different id.

In WordPress, comment_ID() prints the numeric ID of the current comment, for example 12.
It is often used to build an id attribute such as comment-12, so that links like #comment-12 can jump to the comment.
It takes no arguments. Call it inside the comment loop.
comment_ID();
<li id="comment-<?php comment_ID(); ?>"> | Call | What it does |
|---|---|
comment_ID() | Prints the ID of the current comment. |
get_comment_ID() | Returns the ID instead of printing it. |
id="comment-<?php comment_ID(); ?>" | A common way to make a unique id. |
The slug in the URL is lowercase, but the PHP function name is comment_ID().
comment_ID(): void | Parameter | Description |
|---|---|
| None | This function has no parameters. It uses the current comment. |
| Return Value | Description |
|---|---|
| void | Nothing is returned. The function prints the number. |
Three short theme examples. Each one has a sample output.
Give each comment a unique id.
<li id="comment-<?php comment_ID(); ?>"> comment_ID() prints the number.comment- in front makes a valid id.Make a permalink-style anchor to the comment.
<a href="#comment-<?php comment_ID(); ?>">Permalink</a> get_comment_link() for a full URL.Use get_comment_ID() in PHP.
<?php $id = get_comment_ID(); ?>
<div data-comment="<?php echo esc_attr( $id ); ?>"> get_comment_ID() returns the number.esc_attr() makes it safe for an attribute.Print the hidden input fields that connect a reply to its post and parent comment.
14 people found this page helpful