I want to get the page ID before starting the loop in Wordpress. I am using
$page = get_query_var('page_id');
But i returns nothing :(
Apparently I want to check for a page ID and add a class to <body>
tag based on it.
I want to get the page ID before starting the loop in Wordpress. I am using
$page = get_query_var('page_id');
But i returns nothing :(
Apparently I want to check for a page ID and add a class to <body>
tag based on it.
Wordpress Get the Page ID outside the loop
This should help:
Retrieve and Get WordPress Post ID Outside the Loop as PHP Variable
If you're using pretty permalinks, get_query_var('page_id')
won't work.
Instead, get the queried object ID from the global $wp_query
;
global $wp_query;
$page_object = $wp_query->get_queried_object();
$page_id = $wp_query->get_queried_object_id();