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!
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!
Try using this:
$id = get_the_ID();
get_the_ID()
which return error if shown in 404. Thank you :)
You can use $post->ID
to get the current ID.
In most cases, get_the_ID()
will work fine:
$post_id = get_the_ID();
However, in some cases, you may need to use get_queried_object_id()
instead:
$post_id = get_queried_object_id();
The reason for this is that the value returned by get_the_ID()
is context-dependent and, in some cases, may return a different ID than the main post being queried. For further info, I recommend reading WordPress's documentation regarding The Loop.
global $post;
echo $post->ID;
$the_post_ID
, it would be appropriate to use that same variable name in the echo
statement, instead of $thePostID
).
Commented
May 4, 2015 at 6:26
You can get id through below Code...Its Simple and Fast
<?php $post_id = get_the_ID();
echo $post_id;
?>