While I was working on a WordPress template today, I needed to find the current page's post_slug. After investigating the function is_single in wp-includes/query.php, I discovered the following method for determining the current page ID, post title, or post name:
$post_obj = $wp_query->get_queried_object();
$post_ID = $post_obj->ID;
$post_title = $post_obj->post_title;
$post_name = $post_obj->post_name;


[...] keep forgetting this - good thing i stumbled on this tip on finding the current page slug when you are buliding a wordpress [...]
So, after all, what is variable the page slug? is it the post_name?
Hi,
is this still the only way to get the page slug/title before The Loop?
I'm on version 3.3.3.
Hi, Michael!
Thanks for the tip. It helped me a lot!
[]'s
After looking at the source, that does look like the best way to do it. I can't believe there isn't more support for "pages" vs Blogs.
[...] man das machen, wenn einem WordPress schon die ganze Arbeit abnimmt? Danke der Information auf WenderHost.com konnte ich deshalb diesen wirklich kurzen Quelltext auf die Beine stellen, um die ID der aktuellen [...]
[...] needed to get just the post slug for a project I have been working on. I found several sites that had similar ways of getting the post slug but I came up with an approach that utilizes [...]
[...] WordPress Tip: Find the Current Page Slug :: WenderHost. Categories: WordPress Tags: page slug, WordPress 相关文章August 24, 2009 Mon -- [...]
[...] WordPress Tip: Find the Current Page Slug :: WenderHost. Categories: WordPress Tags: current page slug, WordPress 相关文章October 4, 2009 Sun -- [...]
How can I make this work outside of the loop?
What about Pages with a mother page?
Is there a way to get the whole string:
/mother/son/
?
Thanks for the help. It's actually simpler, if all you want is the slug. While inside the loop, you use $post->post_name. For example, you can save the slug as a variable by declaring:
$slug = $post->post_name;
I just used this in a site to dynamically label titles with an id for styling purposes. (There are plenty of other ways to do that, I'm sure, but this worked well for my application).
@Dave or whoever else might be reading:
Michael's original code snippet IS how you get the info outside of the loop. The little snippet I posted above works inside the loop only.
love you man, I was wondering how to get the current page id and other data for like 3 hours!