wordpress get current page id inside and outside of loop

WordPress get current page id or post id is a common query while developing WordPress theme, as there many time it needed to filter or make a link, now the below code will help you to get the current page or post id inside and outside of the loop

WordPress get current page id Outside the loop

To get the post or page id outside of loop users need to design the post or query globally to get the page or post content and then have to write the code to get the id.

Example code is given below

<?php
	global $post;
	echo $post->ID;
?>

or

<?php
global $wp_query;
echo $wp_query->post->ID;
?>

WordPress get current page id Inside the loop

To get a page or post id inside the loop just need to write the code $post->ID; or get_the_ID()

Example code is given below

<?php
	echo $post->ID;
?>

or

<?php
	echo get_the_ID();
?>

In this post, we have seen how to get current page id or post id in WordPress

Please provide your comments to make the site more useful and better.

Thanks for reading

Share The Post On -