174

哪些 PHP 代码可用于检索 WordPress 主题中的当前页面名称?

到目前为止我看到的所有解决方案:

  • the_title()
  • get_page()->post_name
  • get_post()
  • 等等

但这些不适用于包含帖子条目的页面。它们都将返回最新博客条目的名称。

换句话说,假设您在 WordPress 中创建了一个名为“我的新闻”的页面。该页面被设置为“发布页面”。在页面上添加几个帖子。现在,可以使用什么 API 来检索字符串“my-news”而不是最新帖子的名称?


我发现以下变量似乎有效。

$wp_query->queried_object->post_name

这实际上是页面名称 (slug) 的 URL 友好版本,这也是我一直在寻找的。这是使用默认模板(二十个)测试的。我真的不确定为什么下面给出的两个变量在我的网站上不起作用。感谢keatchprint_r()提示

现在,为什么这些信息隐藏得如此之深?

4

19 回答 19

201

WordPress 全局变量$pagename应该可供您使用。我刚刚尝试了您指定的相同设置。

$pagename在文件 wp-includes/theme.php 中定义,在函数内部get_page_template(),当然在解析页面主题文件之前调用它,因此它在页面模板中的任何位置都可用。

  • 尽管似乎没有记录,但$pagename仅在使用永久链接时才设置 var。我想这是因为如果你不使用它们,WordPress 不需要页面 slug,所以它不会设置它。

  • $pagename如果您将页面用作静态首页,则不设置。

  • $pagename这是/wp-includes/theme.php里面的代码,它使用了你在无法设置时指出的解决方案:

--

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;
}
于 2011-01-29T15:58:28.283 回答
44

我获取页面名称的方法:

$slug = basename(get_permalink());
于 2011-09-30T11:10:10.793 回答
27
<?php wp_title(''); ?>

这对我有用。

如果我理解正确,您希望在包含帖子条目的页面上获取页面名称。

于 2011-03-10T17:24:54.030 回答
25

好的,您必须在循环之前获取页面标题。

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

检查参考:http ://codex.wordpress.org/Function_Reference/WP_Query#Properties 。

做一个

print_r($wp_query)

在循环之前查看$wp_query对象的所有值。

于 2011-01-29T15:09:59.807 回答
12

您可以使用全局变量获取当前页面、帖子或自定义帖子类型$post

echo $post->post_title

注意:在函数或类中,您需要global $post;在尝试使用之前指定$post.

如果您的页面上有循环,请确保结束每个循环wp_reset_postdata();以设置$post回正在显示的默认项目(页面)。

请注意,“post_title”变量也可用于任何自定义循环/查询……包括菜单项和媒体附件……WordPress 中的所有内容都是“帖子”。

于 2012-03-09T02:46:23.873 回答
9

如果您希望从您的 functions.php 文件中访问当前页面(因此,循环之前$post、填充之前$wp_query、初始化之前等...)您真的别无选择,只能访问服务器变量本身和从查询字符串中提取请求的页面。

$page_slug = trim( $_SERVER["REQUEST_URI"] , '/' )

请注意,这是一个“愚蠢”的解决方案。它不知道,例如带有 slug 'coming-soon' 的页面也是p=6. 并且它假定您的永久链接设置已设置为pagename(无论如何它们都应该如此!)。

不过,如果您有一个受控的场景,这仍然是一个有用的小技巧。我在希望将未登录的访问者重定向到“即将推出”页面的情况下使用它;但我必须确保我没有将它们扔进可怕的“重定向循环”,所以我需要从这条规则中排除“即将推出”页面:

global $pagenow;
if (
        ! is_admin() &&
        'wp-login.php' != $pagenow &&
        'coming-soon' != trim( $_SERVER["REQUEST_URI"] , '/' ) &&
        ! is_user_logged_in()
){
   wp_safe_redirect( 'coming-soon' );
}
于 2013-04-04T20:57:31.067 回答
7

我相信Roots 入门主题有一个很棒的功能来获取当前页面标题。它非常易于破解,涵盖所有基础,并且可以很容易地与wp_title钩子一起使用。

/**
 * 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();
  }
}
于 2014-02-27T13:06:08.437 回答
7

我们只需要使用“post”全局变量:

global $post;
echo $post->post_title;

这将回显当前页面/帖子标题。

于 2014-05-22T13:22:09.860 回答
6

试试这个:

$pagename = get_query_var('pagename');
于 2012-09-10T10:41:22.010 回答
5

我想出了一个更简单的解决方案。

从 wp_title() 中获取页面名称的返回值。如果为空,则打印主页名称,否则回显 wp_title() 值。

<?php $title = wp_title('', false); ?>

请记住删除与第一个参数的分隔,然后将 display 设置为 false 以用作变量的输入。然后只需在标题等标签之间插入代码。

<?php if ( $title == "" ) : echo "Home"; else : echo $title; endif; ?>

它对我来说是一种享受,并确保在您希望提取的部分中声明第一个$title,这可以调整为返回不同的变量。

于 2013-09-09T02:42:46.807 回答
4

采用:

$title = get_the_title($post);
$parent_title = get_the_title($post->post_parent);

echo $title;
echo $parent_title;
于 2011-10-27T18:40:25.627 回答
3

这似乎是最容易使用的:

<?php single_post_title(); ?>
于 2014-01-29T22:47:43.043 回答
3

在循环开始之前显示标题:

$page_title = $wp_query->post->post_title;
于 2017-05-24T05:54:29.413 回答
2

如果您正在寻找实际查询的页面,而不是页面 ID 或 slug,则一种选择是拦截查询:

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

在您的函数中,您可以访问 $wp 对象,并且可以通过以下方式获取页面名称或帖子名称:

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'];
     }
}

另一方面,如果您确实需要发布数据,那么首先获取它(并且可以说在这种情况下是最好的)是:

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

function show_page_name($wp){
     if ( ! is_admin() ){
        global $post;
        echo $post->ID, " : ", $post->post_name;
     }
}

最后,我意识到这可能不是 OP 的问题,但如果您正在寻找Admin页面名称,请使用全局$pagenow.

于 2011-09-02T20:57:57.013 回答
2

WordPress循环中:

if ( have_posts() ) : while ( have_posts() ) : the_post();
/******************************************/
echo get_the_title();
/******************************************/
endwhile; endif;

这将显示当前页面标题。

供参考:get_the_title()

于 2013-12-19T08:03:41.463 回答
1

这是我的版本:

$title = ucwords(str_replace('-', ' ', get_query_var('pagename')));

get_query_var('pagename') 只是给了我页面蛞蝓。所以上面替换了所有的破折号,并使每个单词的第一个字母大写 - 所以它实际上可以用作标题。

于 2014-02-07T16:09:02.133 回答
0

我现在在WordPress Codec上找到了这个功能,

被查询

这是$wp_query->get_queried_object.

这篇文章让我朝着正确的方向前进,但似乎它需要这个更新。

于 2012-05-23T15:12:27.217 回答
0

如果您在 functions.php 中,这也有效。这不是最好的方法,因为您必须使用全局数组,但它确实有效。

  1. 首先,我们需要添加一个过滤器。必须存在比 template_include 更好的过滤器,但我不知道所有这些。请指点我正确的那个。

    add_filter( 'template_include', 'var_template_include', 1000 );
    function var_template_include( $template ){
        global $wp_query;
        $GLOBALS['current_page'] = $wp_query->get_queried_object()->post_name;
        return $template;
    }
    
  2. 避免直接使用变量

    function get_current_page( $echo =  false ) {
        if( !isset( $GLOBALS['current_page'] ) )
            return false;
        return $GLOBALS['current_page'];
    }
    
  3. 现在您可以在functions.phpget_current_page()的任何其他部分使用该函数。

于 2013-06-21T06:53:22.773 回答
0

这是我最终使用的,截至 2018 年:

<section id="top-<?=(is_front_page() ? 'home' : basename(get_permalink()));?>">
于 2018-12-06T09:47:28.263 回答