[php] Getting the WordPress Post ID of current post

Anyone know how I can get the post ID of the current page?

So, if I'm on a particular post, inside my header.php, I want to be able to get the current post id.

Thanks!

This question is related to php wordpress

The answer is


you can use $post->ID for current id.


You can get id through below Code...Its Simple and Fast

 <?php $post_id = get_the_ID();
   echo $post_id;
   ?>

Try:

$post = $wp_query->post;

Then pass the function:

$post->ID

global $post;
echo $post->ID;

In some cases, such as when you're outside The Loop, you may need to use get_queried_object_id() instead of get_the_ID().

$postID = get_queried_object_id();