Getting the WordPress Post ID of current post

前端 未结 6 1996
挽巷
挽巷 2020-12-08 06:57

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.

相关标签:
6条回答
  • 2020-12-08 07:25

    Try using this:

    $id = get_the_ID();
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-12-08 07:29

    Try:

    $post = $wp_query->post;
    

    Then pass the function:

    $post->ID
    
    0 讨论(0)
  • 2020-12-08 07:33

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

     <?php $post_id = get_the_ID();
       echo $post_id;
       ?>
    
    0 讨论(0)
  • 2020-12-08 07:37

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

    0 讨论(0)
  • 2020-12-08 07:38
    global $post;
    echo $post->ID;
    
    0 讨论(0)
提交回复
热议问题