What php code can be used to retrieve the current page name in a WordPress theme?
All the solutions I have seen so far (the_title()
, get_page()->post_name
, get_post()
, etc) don't work for a page that contains post entries. They will all return the name of the latest blog entry.
Stated another way, assume that you have a page created in WordPress with the name "My News". This page is set as the "post page". Add a couple of posts to the page. Now, what API can be used to retrieve the string "my-news" instead of the name of the latest post?
Edit:
I've found the following variable which seems to work.
$wp_query->queried_object->post_name
This is actually the URL friendly version of the page name (slug), which is what I was looking for too. This was tested with the default template (twentyten). I'm really not sure why the two variables given below do not work on my site. Thanks keatch for the print_r()
tip.
Now, why is this info hidden so deep down?
The WP global variable
$pagename
should be available for you, I have just tried with the same setup you specified.$pagename
is defined in the file wp-includes/theme.php, inside the functionget_page_template()
, which is of course called before your page theme files are parsed, so it is available at any point inside your templates for pages.EDIT:
Although it doesn't appear to be documented, the
$pagename
var is only set if you use permalinks. I guess this is because if you don't use them, WP doesn't need the page slug, so it doesn't set it up.$pagename
is not set if you use the page as a static front page.This is the code inside /wp-includes/theme.php, which uses the solution you pointed out when
$pagename
can't be set:My Approach to get the slug name of the page
Within Loop
This will show you the current page title. For refrence http://codex.wordpress.org/Function_Reference/get_the_title
This also works if you are in the functions.php. It is not the best approach since you have to use the global array but it works.
1- First, we need to add a filter. There must exist a better filter to use than the template_include but I don't know all of them. Please point me to the right one.
2- Avoid using the variable directly
3- Now you can use the function
get_current_page()
in any other part of the functions.php.If you're looking to access the current page from within your functions.php file (so, before the loop, before
$post
is populated, before$wp_query
is initialized, etc...) you really have no choice but to access the server variables themselves and extract the requested page from the query string.Note that this is a "dumb" solution. It doesn't know, for instance that the page with the slug 'coming-soon' is also
p=6
. And it assumes that your permalink settings are set topagename
(which they should be anyway!).Still, can be a useful little trick if you have a controlled scenario. I'm using this in a situation where I wish to redirect non-logged in visitors to a "coming soon" page; but I have to make sure that I'm not throwing them into the dreaded "redirect loop", so I need to exclude the "coming soon" page from this rule:
One option, if you're looking for the actual queried page, rather than the page ID or slug is to intercept the query:
Within your function, you have access to the $wp object and you can get either the pagename or the post name with:
If, on the other hand, you really need the post data, the first place to get it (and arguably in this context, the best) is:
Finally, I realize this probably wasn't the OP's question, but if you're looking for the Admin page name, use the global
$pagenow
.