In the wordpress loop section, each posts displayed on the page with defined format along with such criteria decided by administrator. The Loop template tag the_ID can be used to easily display the post’s ID inside the post.
Alternatively, inside the the_content filter, declare $id global and use $id to retrieve the post ID. the_ID template tag can be used in this format:
Many time user requires to get Post ID outside the loop template tag. In such cases, the_ID function can not work. In that case, $post->ID will be used to return the post ID. $post is a global object that holds various information about the posts displayed on the page. If the $post is used inside a function, the $post has to be declared as a global variable. For example::
Works inside of the Loop
// Works inside of the Loop function function_name() { global $post; $thePostID = $post->ID; }
Works in single post outside of the Loop
// Works in single post outside of the Loop function function_name() { global $wp_query; $thePostID = $wp_query->post->ID; }
Revisions
There are no revisions for this post.
No comments yet.