Ber*_*ard 159 wordpress wordpress-theming
什么PHP代码可用于检索WordPress主题中的当前页面名称?
到目前为止,我所看到的所有解决方案(the_title()
,get_page()->post_name
,get_post()
等)不为包含交条目的页面工作.他们都将返回最新博客条目的名称.
换句话说,假设您在WordPress中创建了一个名为"My News"的页面.此页面设置为"帖子页面".在页面中添加几个帖子.现在,可以使用什么API来检索字符串"my-news"而不是最新帖子的名称?
编辑:
我发现以下变量似乎有效.
$wp_query->queried_object->post_name
Run Code Online (Sandbox Code Playgroud)
这实际上是页面名称(slug)的URL友好版本,这也是我正在寻找的.这是使用默认模板(二十)测试的.我真的不确定为什么下面给出的两个变量在我的网站上不起作用.感谢keatch的print_r()
提示.
现在,为什么这个信息隐藏得如此之深?
AJJ*_*AJJ 179
WP全局变量$pagename
应该可供您使用,我刚刚尝试使用您指定的相同设置.
$pagename
在函数内部的文件wp-includes/theme.php中定义get_page_template()
,当然在解析页面主题文件之前调用它,因此它可以在模板中用于页面的任何位置.
编辑:
虽然它似乎没有记录,但$pagename
只有在使用永久链接时才设置var.我想这是因为如果你不使用它们,WP不需要页面slug,所以它不会设置它.
$pagename
如果您将页面用作静态首页,则不会设置.
这是/wp-includes/theme.php中的代码,它使用了$pagename
无法设置时指出的解决方案:
$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;
}
Run Code Online (Sandbox Code Playgroud)gok*_*sel 37
我获取页面的slug名称的方法
$slug = basename(get_permalink());
Run Code Online (Sandbox Code Playgroud)
kea*_*tch 23
好的,你必须在循环之前获取页面标题.
$page_title = $wp_query->post->post_title;
Run Code Online (Sandbox Code Playgroud)
检查参考:http://codex.wordpress.org/Function_Reference/WP_Query#Properties.
做一个
print_r($wp_query)
Run Code Online (Sandbox Code Playgroud)
在循环之前查看$ wp_query对象的所有值
Gre*_*egB 22
<?php wp_title(''); ?>
Run Code Online (Sandbox Code Playgroud)
这对我有用,如果我理解正确,你想在一个有帖子条目的页面上获取页面名称?
您可以使用全局变量$ post获取当前页面,帖子或自定义帖子类型.
echo $post->post_title
注意:在函数或类中,您需要global $post;
在尝试使用$ post之前指定.
如果页面上有循环,请确保结束每个循环wp_reset_postdata();
以将$ post设置回显示的默认项目(页面).
注意,'post_title'变量也可用于任何自定义循环/查询..包括菜单项和媒体附件...... WordPress中的所有内容都是"帖子".
如果你想从functions.php文件中访问当前页面(那么,在循环之前$post
,填充之前$wp_query
,初始化之前等等)你真的别无选择,只能自己访问服务器变量和从查询字符串中提取请求的页面.
$page_slug = trim( $_SERVER["REQUEST_URI"] , '/' )
Run Code Online (Sandbox Code Playgroud)
请注意,这是一个"愚蠢"的解决方案.它不知道,例如,带有slug'即将来临'的页面也是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' );
}
Run Code Online (Sandbox Code Playgroud)
我相信Roots入门主题有一个很棒的功能来获取当前页面标题,非常黑客,覆盖所有基础,并可以很容易地与wp_title钩子一起使用
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();
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
我想出了一个更简单的解决方案.
从wp_title()获取页面名称的返回值,如果为空,则打印主页名称,否则回显wp_title值.
<?php $title = wp_title('',false); ?>
Run Code Online (Sandbox Code Playgroud)
请记住使用第一个arg删除分隔,然后将display设置为false以用作变量的输入.然后在标题等标签之间插入代码.
<?php if ( $title == "" ) : echo "Home"; else : echo $title; endif; ?>
Run Code Online (Sandbox Code Playgroud)
为我工作并确保在您希望提取$ title的部分中声明第一个,这可以调整为返回不同的变量.
sean@loft6.co.uk
我们只需要使用"post"全局变量.
global $post;
echo $post->post_title;
Run Code Online (Sandbox Code Playgroud)
这将回显当前页面/帖子标题.
希望这有帮助!!!!
归档时间: |
|
查看次数: |
372873 次 |
最近记录: |