WordPress comment_ID() Function

Beginner
3 min read
Updated: Nov 2026
3 examples
Template tag

Definition

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.

Syntax

It takes no arguments. Call it inside the comment loop.

WordPress
comment_ID();
<li id="comment-<?php comment_ID(); ?>">
CallWhat 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().

Full declaration
WordPress
comment_ID(): void

Parameters

ParameterDescription
NoneThis function has no parameters. It uses the current comment.

Return Value

Return ValueDescription
voidNothing is returned. The function prints the number.

WordPress Examples

Three short theme examples. Each one has a sample output.

Example 1 — Make an id

Give each comment a unique id.

WordPress
<li id="comment-<?php comment_ID(); ?>">

How It Works

  1. comment_ID() prints the number.
  2. The word comment- in front makes a valid id.
  3. Each comment gets a different id.

Example 2 — Link to a comment

Make a permalink-style anchor to the comment.

WordPress
<a href="#comment-<?php comment_ID(); ?>">Permalink</a>

How It Works

  1. The link jumps to the element with the same id.
  2. It works on the same page.
  3. Use get_comment_link() for a full URL.

Example 3 — Return value

Use get_comment_ID() in PHP.

WordPress
<?php $id = get_comment_ID(); ?>
<div data-comment="<?php echo esc_attr( $id ); ?>">

How It Works

  1. get_comment_ID() returns the number.
  2. esc_attr() makes it safe for an attribute.
  3. You can use the value for data attributes or checks.

Frequently Asked Questions

It prints the numeric ID of the current comment.
No. It always uses the current comment.
Use get_comment_ID().
It makes each comment easy to link to, for example #comment-12.
Inside the comment loop, such as a wp_list_comments callback.

Next: comment_id_fields()

Print the hidden input fields that connect a reply to its post and parent comment.

comment_id_fields() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful