the_ID()WP 0.71

Display the ID of the current post in the WordPress Loop.

Post ID in the loop can be also obtained through $post->ID. Sometimes it's easier and there is no unnecessary call of the function.

1 time — 0.000403 sec (fast) | 50000 times — 0.18 sec (very fast) | PHP 7.1.2, WP 4.7.3

No Hooks.

Return

null. Nothing (null). The post ID is displayed immediately.

Usage

<?php the_ID(); ?>

Examples

0

#1 Let's print the inscription "Post Number: 465" on the screen (for post 465)

<p>Post Number: <?php the_ID(); ?></p>
0

#2 HTML Anchor of the post

<h3 id="post-<?php the_ID(); ?>"><?php the_title(); ?></h3>

Now the link to this anchor will be: http://site.com/link_to_post#post-465

in XHTML, the id attribute of an HTML tag cannot start with a digit. Therefore, you need to add at least one letter before the_ID().

Changelog

Since 0.71 Introduced.

the_ID() code WP 6.1.1

function the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
	echo get_the_ID();
}