What php code may be used to retrieve the present page title inside a Wordpress theme?

All of the solutions I've come across to date (the_title(), get_page()->post_title, get_publish(), ...) aren't effective for any page that consists of publish records. Everyone will return the title from the latest blog entry.

Mentioned one other way, assume that you've a page produced in Wordpress using the title "My News". This site is placed because the "publish page". Add a few posts the page. Now, what API may be used to retrieve the string "my-news" rather than the title from the latest publish?

Edit:

I have found the next variable which appears to operate.

$wordpress_query->queried_object->post_title

This really is really the URL friendly version from the page title (slug), that is things i was searching for too. It was examined using the default template (twentyten). I am not really sure why the 2 variables given below fail to work on my small site. Thanks keatch for that print_r() tip.

Now, why info hidden so deep-down?

The Wordpress global variable $pagename ought to be for you, I've just attempted with similar setup you specified.

$pagename is determined within the file wordpress-includes/theme.php, within the function get_page_template(), which is always known as before your page theme files are parsed, so it's available at any time within your templates for pages.

EDIT:

  • Even though it does not seem to be recorded, the $pagename var is just set if you are using permalinks. I suppose the reason being if you do not rely on them, Wordpress does not require the page slug, therefore it does not arrange it.

  • $pagename isn't set if you are using the page like a static top of the page.

  • This is actually the code inside /wordpress-includes/theme.php, which uses the answer you stated when $pagename can not be set:

    $pagename = get_query_var('pagename')
    if ( !$pagename &lifier&lifier $id > ) If your static page is placed because the top of the page, $pagename won't be set. Retrieve it in the queried object
    $publish = $wordpress_query->get_queried_object()
    $pagename = $publish->post_title

Ok, you have to grab the page title Prior to the loop.

$page_title = $wordpress_query->post->post_title

Look for reference: http://codex.wordpress.org/Function_Reference/Wordpress_Query#Qualities.

Perform a

print_r($wordpress_query)

prior to the loop to determine all of the values of $wordpress_query object

<?php wordpress_title('') ?>

This labored for me personally, basically understand properly, you need to obtain the page title on the page which has publish records?

Also to find the slug title from the page $slug = basename(get_permalink())

One option, if you are searching for the particular queried page, as opposed to the page ID or slug would be to intercept the query:

add_action('parse_request', 'show_query', 10, 1)

In your function, you can get the $wordpress object and you will get either the pagename or even the publish title with:

function show_query($wordpress)wordpress->query_vars['pagename']

         echo $wordpress->query_vars['name']

     



If, however, you actually need the publish data, the to begin with to have it (and perhaps within this context, the very best) is:

add_action('wp', 'show_page_name', 10, 1)

function show_page_title($wordpress)publish

        echo $publish->ID, " : ", $publish->post_title

     




    $title = get_the_title($publish)

    $parent_title = get_the_title($publish->post_parent)

    echo $title

    echo $parent_title

Hope this helps !! )