Getting the WordPress Post ID of current post

2019-01-22 12:02发布

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!

标签: php wordpress
5条回答
Root(大扎)
2楼-- · 2019-01-22 12:13

Try:

$post = $wp_query->post;

Then pass the function:

$post->ID
查看更多
【Aperson】
3楼-- · 2019-01-22 12:21

Try using this:

$id = get_the_ID();
查看更多
劳资没心,怎么记你
4楼-- · 2019-01-22 12:24

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

查看更多
Fickle 薄情
5楼-- · 2019-01-22 12:26

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

 <?php $post_id = get_the_ID();
   echo $post_id;
   ?>
查看更多
劳资没心,怎么记你
6楼-- · 2019-01-22 12:28
global $post;
echo $post->ID;
查看更多
登录 后发表回答