php 获取当前帖子的 WordPress 帖子 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4893435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the WordPress Post ID of current post
提问by dzm
Anyone know how I can get the post ID of the current page?
任何人都知道如何获取当前页面的帖子 ID?
So, if I'm on a particular post, inside my header.php, I want to be able to get the current post id.
因此,如果我在某个特定的帖子上,在我的 header.php 中,我希望能够获得当前的帖子 ID。
Thanks!
谢谢!
回答by Wade Tandy
Try using this:
尝试使用这个:
$id = get_the_ID();
回答by borayeris
you can use $post->ID for current id.
您可以使用 $post->ID 作为当前 ID。
回答by Anoop Saini
global $post;
echo $post->ID;
回答by Rajnikanth
Try:
尝试:
$post = $wp_query->post;
Then pass the function:
然后传递函数:
$post->ID
回答by Pikamander2
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()
.
在某些情况下,例如当您在 The Loop 之外时,您可能需要使用get_queried_object_id()
代替get_the_ID()
。
$postID = get_queried_object_id();
回答by Mohammad Farhan Atif Wattoo
You can get id through below Code...Its Simple and Fast
您可以通过下面的代码获取 id...它简单快捷
<?php $post_id = get_the_ID();
echo $post_id;
?>