tags:

views:

36

answers:

3

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.

A: 

You can use is_page($page_id) outside the loop to check.

nikc
I dont want to check a page, I want to get the ID of current page.
atif089
@atif are you sure a page ID is in fact being passed? You don't happen to be on the front page?
Pekka
yes I did check it
atif089
A: 

Wordpress Get the Page ID outside the loop

This should help:

Retrieve and Get WordPress Post ID Outside the Loop as PHP Variable

Sarfraz
tried this already, isnt working..
atif089
@atif089: Is there any error you get with this? Or are you using htacces for url rewriting?
Sarfraz
no but query isnt passed i guess. print_r($GET) seems to be an empty array
atif089
+2  A: 

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();
TheDeadMedic
Oh yes I was using pretty permalinks :)
atif089