WordPress Get the Page ID outside the loop
Asked 07 September, 2021
Viewed 1.7K times
  • 55
Votes

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.

11 Answer