WordPress at a glance

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.

Works based on: get_the_ID()
✈ 1 time = 0.000403s = fast | 50000 times = 0.18s = very fast PHP 7.1.2, WP 4.7.3

No Hooks.

Return

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

Usage

<?php the_ID(); ?>

Examples

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

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

#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().

Code of the_ID: wp-includes/post-template.php VER 4.9.8

<?php
function the_ID() {
	echo get_the_ID();
}

Related Functions

From tag: WP Loop

More from category: Posts, pages...

More from Template Tags: Posts, Pages, ...

No comments
    Hello, !     Log In . Register