This post was last updated on January 15th, 2017 at 06:04 pm
In WordPress we’re not always working inside the WordPress loop. Working inside the loop, we use the template tag
the_ID, which easily get current page ID. But while working outside the loop then getting current page ID is bit tricky.
Get Current Page ID Inside The Loop
$page_id = the_ID();
return numerical ID of the current post or page in the WordPress Loop. False if $post is not set.
Get Current Page ID Outside The Loop
Let’s look at how this is done. To get page id outside the loop we can make use of
get_queried_object() here, which is a wrapper for $wp_query and returns the whole post metadata.
global $wp_query; $page = $wp_query->get_queried_object(); $page_id = $page->ID; //OR $page_id = get_queried_object_id();