I want to get the page ID before starting the loop in Wordpress. I am using
$page = get_query_var('page_id');
Apparently, it returns nothing.
I just want to check a page for its 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');
Apparently, it returns nothing.
I just want to check a page for its ID and add a class to <body>
tag based on it.
This is the correct code.
Use this global $post instead:
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
Use below two lines of code to get current page or post ID
If you are out of the Loop of WordPress you can not use any of the method of wordpress so you must use pure php.
You can use this code. And sure will help you :)
Enjoy!
You can use
is_page($page_id)
outside the loop to check.