How to get the current page name in WordPress?

2019-01-05 09:51发布

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?

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?

19条回答
狗以群分
2楼-- · 2019-01-05 10:05

This is what I ended up using, as of 2018:

<section id="top-<?=(is_front_page() ? 'home' : basename(get_permalink()));?>">
查看更多
爷、活的狠高调
3楼-- · 2019-01-05 10:08

I know this is old, but I came across this and what seems to be the easiest is using this.

<?php single_post_title(); ?>
查看更多
该账号已被封号
4楼-- · 2019-01-05 10:09

I believe that the Roots starter theme has a fantastic function to get the current page title, very hackable, covers all bases and can be easily used with the wp_title hook

https://github.com/roots/roots/blob/6.5.0/lib/titles.php.

/**
 * Page titles
 */
function roots_title() {
  if (is_home()) {
    if (get_option('page_for_posts', true)) {
      echo get_the_title(get_option('page_for_posts', true));
    } else {
      _e('Latest Posts', 'roots');
    }
  } elseif (is_archive()) {
    $term = get_term_by('slug', get_query_var('term'), get_query_var('taxonomy'));
    if ($term) {
      echo $term->name;
    } elseif (is_post_type_archive()) {
      echo get_queried_object()->labels->name;
    } elseif (is_day()) {
      printf(__('Daily Archives: %s', 'roots'), get_the_date());
    } elseif (is_month()) {
      printf(__('Monthly Archives: %s', 'roots'), get_the_date('F Y'));
    } elseif (is_year()) {
      printf(__('Yearly Archives: %s', 'roots'), get_the_date('Y'));
    } elseif (is_author()) {
      $author = get_queried_object();
      printf(__('Author Archives: %s', 'roots'), $author->display_name);
    } else {
      single_cat_title();
    }
  } elseif (is_search()) {
    printf(__('Search Results for %s', 'roots'), get_search_query());
  } elseif (is_404()) {
    _e('Not Found', 'roots');
  } else {
    the_title();
  }
}
查看更多
对你真心纯属浪费
5楼-- · 2019-01-05 10:10

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.

查看更多
够拽才男人
6楼-- · 2019-01-05 10:13

Show title before the loop starts:

$page_title = $wp_query->post->post_title;

Thank you

查看更多
别忘想泡老子
7楼-- · 2019-01-05 10:19

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

查看更多
登录 后发表回答