php 获取页面名字,如何在WordPress中获取当前页面名称?

WP全局variables$pagename应该是可用的,我刚刚尝试过与您指定的相同的设置。

$pagename是在函数get_page_template()内的文件wp-includes / theme.php中定义的,当然在页面主题文件被parsing之前调用它,因此它可以在页面模板中的任意位置使用。

编辑:

虽然它似乎没有logging, $pagename var只在使用永久链接时才会设置。 我想这是因为如果你不使用它们,WP不需要页面slu so,所以它不会设置它。

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

这是/wp-includes/theme.php中的代码,它使用您在$pagename无法设置时指出的解决scheme:

$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; }

我的方法来获取网页的slu name名称

$slug = basename(get_permalink());

好的,你必须抓住循环前的页面标题。

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

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

做一个

print_r($wp_query)

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

这对我来说,如果我理解正确,你想获得页面名称的页面上有post的条目?

您可以使用全局variables$ post获取当前页面,发布或自定义posttypes。

echo $post->post_title

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

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

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

如果你想从你的functions.php文件中访问当前页面(所以, 在循环之前,在填充$post之前, $wp_query被初始化之前,等等),你实在别无select,只能访问服务器variables本身并从查询string中提取请求的页面。

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

请注意,这是一个“愚蠢的”解决scheme。 它不知道,例如,slu'“即将到来”的页面也是p=6 。 它假定您的永久链接设置设置为pagename (他们应该是无论如何!)。

如果你有一个可控的场景,那么可以是一个有用的小技巧。 我正在使用这种情况,我希望将未login的访问者redirect到即将到来的页面; 但我必须确定我没有把它们扔到可怕的“redirect循环”中,所以我需要从这个规则中排除“即将到来”的页面:

global $pagenow; if ( ! is_admin() && 'wp-login.php' != $pagenow && 'coming-soon' != trim( $_SERVER["REQUEST_URI"] , '/' ) && ! is_user_logged_in() ){ wp_safe_redirect( 'coming-soon' ); }

尝试这个:

$pagename = get_query_var('pagename');

$ title = get_the_title($ post);

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

echo $ title;

echo $ parent_title;

希望这会有所帮助! ;)

我相信Roots的入门主题有一个奇妙的function来获得当前页面的标题,非常可笑,覆盖所有的基础,并可以很容易地使用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(); } }

我们只需要使用“post”全局variables。

global $post; echo $post->post_title;

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

希望这个帮助!

我已经想出了一个更简单的解决scheme。

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

请记住用第一个参数删除分隔,然后将显示设置为false以用作variables的input。 然后只是在你的标题等标签之间插入代码。

为我工作,保证第一个在你想要提取$ title的部分声明,这可以调整返回不同的variables。

在循环内

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

我知道这是旧的,但我碰到这个,似乎是最简单的是使用这个。

一个选项,如果你正在寻找实际查询的页面,而不是页面ID或slu is是拦截查询:

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

在你的函数中,你可以访问$ wp对象,你可以通过以下方式获得页面名称或者post名称:

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的问题,但如果你正在寻找pipe理页面的名称,使用全局$pagenow 。

这是我的版本:

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

get_query_var('pagename')只是给我的页面slu 。 所以上面的代码将会replace所有的破折号,并且将每个单词的第一个字母大写 – 所以它实际上可以用作标题。

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

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

谢谢

我现在在WordPress Codecfind了这个函数:

这是$wp_query->get_queried_object的包装器。 这个职位让我在正确的方向,但似乎需要这个更新。

这也适用于如果你在functions.php。 这不是最好的方法,因为你必须使用全局数组,但它的工作原理。

1-首先,我们需要添加一个filter。 必须存在一个比template_include更好的filter,但我不知道它们全部。 请把我指向正确的一个。

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-避免直接使用variables

function get_current_page( $echo = false ) { if( !isset( $GLOBALS['current_page'] ) ) return false; return $GLOBALS['current_page']; }

3-现在你可以在functions.php的其他部分使用函数get_current_page() 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值