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().
Changelog
Since 0.71 | Introduced. |
Code of the_ID() the ID WP 5.6
function the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
echo get_the_ID();
}Related Functions
From tag: WP Loop
More from category: Posts, pages...
- edit_post_link()
- get_delete_post_link()
- get_edit_post_link()
- get_permalink()
- get_post_field()
- get_post_status()
- get_post_time()
- get_sample_permalink()
More from Template Tags: Posts, Pages, ...
- body_class()
- get_post_format()
- have_posts()
- in_the_loop()
- next_image_link()
- next_post_link()
- next_posts_link()