如何获取用于呈现当前页面的文件的名称? [英] How do I get the name of the file that is being used to render the current page?
问题描述
假设我安装了 WordPress,页面名为关于".如果我转到 http://example.com/about
,我会从 WordPress 的模板层次结构页面知道我正在查看主题文件 page.php
.
Let's say I have a WordPress installation, with a page named "About". If I go to http://example.com/about
, I know from WordPress' template hierarchy page that I'm looking at the theme file page.php
.
我想知道是否有办法在页面某处显示该事实(用于主题调试)?就像我会调用什么函数(或代码)来显示用于呈现我正在查看的页面的当前 PHP 页面.
I'm wondering if there's a way to display that fact (for theme debugging) on the page somewhere? Like what function (or code) would I call to display the current PHP page that is being used to render the page I'm looking at.
我可以用 $_SERVER['PHP_SELF']
做一些事情,但我正在寻找一种不必编辑每个 PHP 文件的方法.就像在调用页面时吐出它正在使用的文件列表的东西一样.
I could do something with $_SERVER['PHP_SELF']
, but I'm looking for a way where I don't have to edit every PHP file. Like something that spits out the list of files it's using as the pages are called.
推荐答案
可以在Html源码中这样打印:
It can be printed in the Html source code like this:
add_action( 'wp_head', 'so_9405896_show_template', 999 );
function so_9405896_show_template() {
global $template;
echo '
<!--
TEMPLATE = ' . basename($template) .'
-->
';
}
或者为了更容易的可视化,直接在内容中使用:
Or for easier visualization, directly in the content with this:
add_filter( 'the_content', 'so_9405896_the_content_filter', 20, 1 );
function so_9405896_the_content_filter( $content )
{
if( is_admin() || !current_user_can( 'administrator' ) )
return $content;
global $template;
$the_templ = '<strong style="background-color: #CCC;padding:10px">TEMPLATE = '
. basename( $template ) . '</strong><br />';
$content = sprintf( $the_templ . '%s', $content );
return $content;
}
结果:
这篇关于如何获取用于呈现当前页面的文件的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!