Home » Questions » All Answers

All Answers

All Answers » Re: How to get the current page name in Wordpress » Comments for "Re: How to get the current page name in Wordpress"
  • Deborah
    $pagename is empty when used inside header.php. I've tried using the default template (twentyten) and using it immediately after the <body> tag but it is not set. Incidentally, none of the query_vars variables seem to be set at that stage.

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

  • 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