Home » Questions » How to get the current page name in Wordpress

How to get the current page name in Wordpress

Answered
1
4

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

  • demo
    Whatever!
  • demo
    Is this plugin fast or slow?
  • You must to post comments
Good Answer
2
2

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
    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

  • Martha
    You could try a slightly different approach using get_query_var('pagename').

    NOTE: This comment was originally posted at StackOverflow.com by AJJ

  • Deborah
    @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

  • Martha
    @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

  • Deborah
    @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

4
0

Also to get the slug name of the page $slug = basename(get_permalink());

NOTE: This answer was originally posted at StackOverflow.com by goksel

  • You must to post comments
1
0

Try this:

$pagename = get_query_var('pagename');

NOTE: This answer was originally posted at StackOverflow.com by user1581293

  • You must to post comments
1
0

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

  • You must to post comments
1
0

    $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

  • You must to post comments
1
0
<?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

  • You must to post comments
1
1

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

  • Deborah
    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

  • Bradley
    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 to post comments
0
0

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

  • You must to post comments
0
0

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

  • You must to post comments
Showing 9 results
Your Answer
Guest Author
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website
CAPTCHA*
Enter the characters shown on the image.