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(), …) 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?
NOTE: This question was originally posted at StackOverflow.com by Alkaline
- Deborah asked 14 years ago
- last edited 14 years ago
-
Whatever!
-
Is this plugin fast or slow?
- You must login to post comments
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 function get_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:$pagename = get_query_var('pagename');
if ( !$pagename && $id > 0 ) {
// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
$post = $wp_query->get_queried_object();
$pagename = $post->post_name;
}
NOTE: This answer was originally posted at StackOverflow.com by AJJ
- Martha answered 14 years ago
- last edited 14 years ago
-
I can't see any reason for the absence of that variable, perhaps I have misunderstood your setup? I understood you are trying to do this inside a page template. Even if you write "echo $pagename" in the first line of header.php it should give you the value. What version of WP are you dealing with?
NOTE: This comment was originally posted at StackOverflow.com by AJJ
-
You could try a slightly different approach using get_query_var('pagename').
NOTE: This comment was originally posted at StackOverflow.com by AJJ
-
@AJweb: Yes, it's for a template that I'm building but I'm using the default template for testing, inside header.php. I get the impression that you haven't tested your solution. Do you see the page name when you put an <h1><?php echo $pagename ?></h1> right after the <body> tag in header.php ?
NOTE: This comment was originally posted at StackOverflow.com by Alkaline
-
@Alkaline: Your are right in one thing: My solutions don't work with all setups of WP. But you are wrong in thinking that I hadn't checked my solutions, I did. I post my findings in an edit of my answer so I can format it properly.
NOTE: This comment was originally posted at StackOverflow.com by AJJ
-
@AJweb: Thanks for the extra research. It adds crucial information. I am using static pages and that explains why $pagename isn't available.
NOTE: This comment was originally posted at StackOverflow.com by Alkaline
- 1 more comment
- You must login to post comments
Also to get the slug name of the page $slug = basename(get_permalink());
NOTE: This answer was originally posted at StackOverflow.com by goksel
- Kathryn answered 13 years ago
- You must login to post comments
Try this:
$pagename = get_query_var('pagename');
NOTE: This answer was originally posted at StackOverflow.com by user1581293
- Michelle answered 12 years ago
- last edited 12 years ago
- You must login to post comments
You can get the current page, post, or custom post type with the global variable $post.
echo $post->post_title
Note: In a function or class you’ll need to specify global $post;
prior to trying to use $post.
If you have loops on your page, make sure you end each loop with wp_reset_postdata();
to set $post back to the default item being displayed (the page).
Note, the ‘post_title’ variable is also available for any custom loop / query.. including menu items and media attachments.. everything in WordPress is a ‘post’.
NOTE: This answer was originally posted at StackOverflow.com by James
- Tina answered 13 years ago
- You must login to post comments
$title = get_the_title($post); $parent_title = get_the_title($post->post_parent); echo $title; echo $parent_title;
Hope this will help !! 😉
NOTE: This answer was originally posted at StackOverflow.com by Jerad Rutnam
- Glenn answered 13 years ago
- You must login to post comments
<?php wp_title(''); ?>
This worked for me, if I understand correctly, you want to get the page name on a page that has post entries?
NOTE: This answer was originally posted at StackOverflow.com by GregB
- Sharon answered 14 years ago
- You must login to post comments
Ok, you must grab the page title BEFORE the loop.
$page_title = $wp_query->post->post_title;
Check for reference: http://codex.wordpress.org/Function_Reference/WP_Query#Properties.
Do a
print_r($wp_query)
before the loop to see all the values of $wp_query object
NOTE: This answer was originally posted at StackOverflow.com by keatch
- Annette answered 14 years ago
-
This doesn't work either. If I print the $wp_query object right after the <body> tag I see that the first post object is the blog entry, not the page itself. However, thanks for the print_r() tip. I could find out the variable that has the info I'm looking for (putting this info in the question).
NOTE: This comment was originally posted at StackOverflow.com by Alkaline
-
echoing out that $page_title var worked for me in the header, thanks.
NOTE: This comment was originally posted at StackOverflow.com by Nicola
- You must login to post comments
I’ve found now in Wordpress Codec
this function:
get queried http://codex.wordpress.org/Function_Reference/wp_list_pages
which is a wrapper for $wp_query->get_queried_object
. This post put me in the right direction but it seems that it needs this update.
NOTE: This answer was originally posted at StackOverflow.com by Sandra
- Danny answered 13 years ago
- last edited 12 years ago
- You must login to post comments
One option, if you’re looking for the actual queried page, rather than the page ID or slug is to intercept the query:
add_action('parse_request', 'show_query', 10, 1);
Within your function, you have access to the $wp object and you can get either the pagename or the post name with:
function show_query($wp){
if ( ! is_admin() ){ // heck we don't need the admin pages
echo $wp->query_vars['pagename'];
echo $wp->query_vars['name'];
}
}
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:
add_action('wp', 'show_page_name', 10, 1);
function show_page_name($wp){
if ( ! is_admin() ){
global $post;
echo $post->ID, " : ", $post->post_name;
}
}
NOTE: This answer was originally posted at StackOverflow.com by Tom Auger
- Anita answered 13 years ago
- last edited 13 years ago
- You must login to post comments