How can I get the current page name in WordPress?

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?


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 (Twenty Ten). I'm really not sure why the two variables given below do not work on my site. Thanks to keatch for the print_r() tip.

Now, why is this information hidden so deep down?

Solutions

My approach to get the slug name of the page:

$slug = basename(get_permalink());

The WordPress 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 is called before your page theme files are parsed, so it is available at any point inside your templates for pages.

  • 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, WordPress 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:

--

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;
}
<?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.

Similar questions

get post content and display in a page by matching the page name and post category name
I am writing some little hacks for wordpress by giving myself some used cases. I think that is the best way to learn this I have created a scenario where I am checking if the page name is 'x' then fetch contents of posts categorized as 'y' in the page x. Works fine as long as I hard code the page names like this Now I am thinking what if I generali...
How to achieve this permalink -> category-name/custom-post-type-name/post-name
I am trying to use categories to provide the structure for my site. I will create sections on my site called England, Wales, Scotland and Ireland . These will be categories. My posts will use custom post types - Video, Guide, Event, Clubs etc. So, if I create a post called 'Trouble down south' using the 'Video' CPT, attach it to the 'England' categ...
Need to display tag group as first name, last name but sort on last name
The reviewers for our site are listed as filtered tags by first name then last name. How would I keep the display list as is but sort by last name? I have now: Aaron Smith Bob Jones Carl Harrison Henry Adams Steven Dunn I would like it to list by last name: Henry Adams Steven Dunn Carl Harrison Bob Jones Aaron Smith
SQL to set Display Name to First Name + Last Name
I'm trying to set the wp_users.display_name for a rather larger users db using SQL query. Got till this point: But trying to change the first SELECT statement to an UPDATE doesn't work:
How can we get display name by last name?
I have a user's last name and want to get their display name. get_user_by() only allows ID, slug, email or login. How can I get display name from last name? On functions.php I have: And I have on page.php: <?php echo get_display_name_by_last_name ('last_name', 'A1e'); ?>
How to get current page name in my wordpress plugin
I am creating a custom wordpress plugin. Where for some pages like http://example.com/clinic http://example.com/pharmacy i will append my template code from my plugin folder. Which mean i will include my custom template not even related to wordpress theme. my code will look like this inside my wordpress plugin page. But $wp-query_vars('pagename') r...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.