Get post ID outside of loop

Adding this PHP code snippet to your single.php will return the current post ID. $post->ID can be called directly outside of the Loop too in a single post template, such as <?php echo $post->ID ?> will print display the post’s ID number.




global $wp_query;
$postID = $wp_query->post->ID;

Be Sociable, Share!

Leave a comment