Home » Php » php – Getting the WordPress Post ID of current post

php – Getting the WordPress Post ID of current post

Posted by: admin April 23, 2020 Leave a comment

Questions:

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!

How to&Answers:

Try using this:

$id = get_the_ID();

Answer:

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

Answer:

Try:

$post = $wp_query->post;

Then pass the function:

$post->ID

Answer:

global $post;
echo $post->ID;

Answer:

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();

Answer:

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

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