您如何找出哪个模板页面正在为当前页面提供服务?
-
-
我检查html并找到一个已识别的标签或某些独特的标签.I inspect the html and find an identified tag or something unique.
- 1
- 2011-12-27
- Naoise Golden
-
查看源代码,并查找可以告诉您使用哪个模板的主体类.还给你的身份证View the source code and look for the body classes which tell you which template is used. Also gives you the i.d.
- 1
- 2014-02-04
- Brad Dalton
-
可能重复的[获取当前模板文件的名称](https://wordpress.stackexchange.com/questions/10537/get-name-of-the-current-template-file)Possible duplicate of [Get name of the current template file](https://wordpress.stackexchange.com/questions/10537/get-name-of-the-current-template-file)
- 0
- 2017-06-13
- Burgi
-
@BradDalton +1.特别是在不允许我们安装插件或编写函数以实现目标的情况下.@BradDalton +1. Specially when we are not allowed to install a plugin or write a function to achieve the goal.
- 0
- 2018-07-13
- Subrata Sarkar
-
9 个回答
- 投票数
-
- 2011-12-26
连接到
template_include
,设置一个全局变量以记录由主题设置的模板,然后将该值读回到页脚或页眉中,以查看为给定视图调用的模板.我之前在">获取当前名称模板文件,但请获取该代码的副本,并对其主题的
functions.php
文件.然后打开主题的
header.php
或footer.php
(或任意位置),并使用类似以下的方法打印当前模板.<div><strong>Current template:</strong> <?php get_current_template( true ); ?></div>
如果您想在生产站点上使用此功能,并使该信息远离非管理员用户,请添加一些条件逻辑.
<?php // If the current user can manage options(ie. an admin) if( current_user_can( 'manage_options' ) ) // Print the saved global printf( '<div><strong>Current template:</strong> %s</div>', get_current_template() ); ?>
现在,您可以跟踪正在使用哪种模板的视图,同时使该信息远离访问者.
Hook onto
template_include
, set a global to note the template set by the theme then read that value back into the footer or header to see which template is being called for a given view.I spoke about this filter hook before in Get name of the current template file, but go grab a copy of that code and plonk it your theme's
functions.php
file.Then open up the theme's
header.php
orfooter.php
(or wherever you like) and use something like the following to print out the current template.<div><strong>Current template:</strong> <?php get_current_template( true ); ?></div>
If you wanted to use this on a production site and keep that info away from your non-administrator users, add a little conditional logic.
<?php // If the current user can manage options(ie. an admin) if( current_user_can( 'manage_options' ) ) // Print the saved global printf( '<div><strong>Current template:</strong> %s</div>', get_current_template() ); ?>
Now you can keep track of what views are using what template, whilst keeping that info away from your visitors.
-
如果此答案有问题,或者任何人都可以对可以改善此答案的方法提出意见,请在此提出评论,并分享您对如何改善它的想法和想法.If there is something wrong with this answer, or if anyone could provide comments on what could be done to improve this answer, have at it, drop a comment here and share your thoughts and ideas on how to make it better.
- 1
- 2014-01-28
- t31os
-
兄弟没有用,它说"未定义函数"It didn't work bro, it says "Undefined function"
- 1
- 2016-04-27
- Lucas Bustamante
-
@LucasB同样在这里,那是我得到的错误@LucasB same here, that's the error I got
- 1
- 2017-01-07
- Lincoln Bergeson
-
这应该是[`get_page_template`](https://codex.wordpress.org/Function_Reference/get_page_template)This should be [`get_page_template`](https://codex.wordpress.org/Function_Reference/get_page_template)
- 2
- 2017-08-11
- Blazemonger
-
`get_current_template`不是一个函数,`get_page_template`不为我打印任何内容(woocommerce页面).`get_current_template` is not a function and `get_page_template` prints nothing for me (a woocommerce page).
- 0
- 2020-06-27
- run_the_race
-
- 2011-12-26
好吧,如果您要做的只是检查已使用哪个模板文件来生成当前页面,那么您就不需要弄脏代码了;)
有一个方便的插件,称为调试栏..在包括您在内的许多情况下,它都是很好的帮助者.您绝对应该检查一下-对我和其他许多人来说,它是任何WP开发的必备伴侣.
我已经附上了一张截图,可以让您坠入爱河...
要使调试栏正常工作,您需要启用
wp_debug
和wp_savequeries
选项.这些选项默认情况下处于禁用状态.在进行任何更改之前,请记住以下几点:
- 请勿在生产环境中执行此操作,除非该网站无法满足大量访问量.
- 完成调试后,请确保禁用网站的选项(尤其是wp_savequeries选项,因为它会影响性能).
进行更改:
- 通过ftp客户端打开
wp_config.php
文件. - 搜索
wp_debug
选项.将其编辑为define( 'WP_DEBUG', true );
.如果该行不存在,请将其添加到文件中. - 类似地,将
define( 'SAVEQUERIES', true );
行添加或添加到文件中. - 保存.您已准备好进行调试.
更多信息:食典
Well, if all you want is to check which template file has been used to generate the current page then you don't need to get your hands dirty with code ;)
There's this handy plugin called Debug Bar. It's a great helper in many situations including yours. You should definitely check it out - for me and many others it's a must-have companion for any WP development.
I've attached a screenshot that could make you fall in love...
To get the Debug Bar working, you need to enable
wp_debug
andwp_savequeries
options. These options are in disabled state by default.Before you make any changes though, there are a few points to keep in mind:
- Do not do it in production environment unless the website doesn't cater to a lot of traffic.
- Once you finish debugging, ensure to disable the options (especially the wp_savequeries option since it affects the performance) of the website.
To make the changes:
- Open
wp_config.php
file through a ftp client. - Search for
wp_debug
option. Edit it todefine( 'WP_DEBUG', true );
. If the line is not present, add it to the file. - Similarly, edit or add the line
define( 'SAVEQUERIES', true );
to the file. - Save. You are ready to debug.
More info: Codex
-
@justCallMeBiru-调试栏插件*不需要* WP_DEBUG和`SAVEQUERIES`,尽管它们被*增强了*.@justCallMeBiru -- the Debug Bar plugin doesn't *require* `WP_DEBUG` and `SAVEQUERIES`, though it is *enhanced* by them.
- 2
- 2014-01-15
- Pat J
-
运行这样的插件,仅需要一点点信息,就会创建大量开销的恕我直言,因此这就是为什么我没有在自己的答案中建议它的原因.就是说,很明显人们喜欢这个答案,我很好奇为什么知道.Running such a plugin, just for one tid bit of information creates alot of overhead imho, and thus it is why i did not suggest it in my own answer. That said, clearly people prefer this answer, i'm curious to know why though.
- 3
- 2014-01-28
- t31os
-
- 2014-01-23
我使用此方便的功能仅显示超级管理员的当前模板:
function show_template() { if( is_super_admin() ){ global $template; print_r($template); } } add_action('wp_footer', 'show_template');
希望有帮助.:)
I use this handy function that displays the current template only for super admins:
function show_template() { if( is_super_admin() ){ global $template; print_r($template); } } add_action('wp_footer', 'show_template');
Hope that helps. :)
-
这是转到答案,应该接受.This is the goto answer, should be accepted.
- 3
- 2018-03-13
- Hybrid Web Dev
-
我也使用它,但是它仍然缺少使用"include"的显示,只显示顶层页面.I use this also but it still lacks the display of which “include” is being used and only shows the top level page.
- 0
- 2020-07-08
- Burndog
-
- 2011-12-27
在每个相关模板文件的get_header行之后添加以下代码:
<!-- <?php echo basename( __FILE__ ); ?> -->
在浏览器中>查看源代码,模板名称将作为注释显示在html代码中,例如
<!-- page.php -->
Add the following code right after the get_header line in each relevant template file:
<!-- <?php echo basename( __FILE__ ); ?> -->
In your browser > view source, and the template name will be displayed as a comment in your html code, e.g.
<!-- page.php -->
-
将其添加到任何地方都需要付出很大的努力it's too much effort to add this everywhere
- 0
- 2019-02-18
- Adal
-
哈哈哈,如果要标记每个文件然后简单地用实际文件名标记它,为什么还要麻烦呢!hahaha, why bother with this if you're going to label each file then simply label it with its actual file name!
- 0
- 2020-05-09
- Aurovrata
-
@Aurovrata很久以前了.有更好的解决方案.但是我有一个简单的脚本,可将代码插入文件夹中所有文件的顶部,因此无需对实际名称进行硬编码.在1或2秒钟内完成.@Aurovrata it was a long time ago. There are way better solutions. But I had a simple script to insert the code at the top of all files in a folder, so no hardcoding of actual names required. Done in 1 or 2 seconds.
- 0
- 2020-05-20
- ronald
-
很公平,:)fair enough, :)
- 0
- 2020-05-21
- Aurovrata
-
- 2017-09-15
您在这里:
HTML列表,其中包含用于当前登录页面的所有模板文件,包括来自插件,子主题和/或父主题组合的所有模板部分,全部放在一行代码中:
echo '<ul><li>'.implode('</li><li>', str_replace(str_replace('\\', '/', ABSPATH).'wp-content/', '', array_slice(str_replace('\\', '/', get_included_files()), (array_search(str_replace('\\', '/', ABSPATH).'wp-includes/template-loader.php', str_replace('\\', '/', get_included_files())) + 1)))).'</li></ul>';
您可能需要检查,以确保服务器在任何路径下均不返回双斜杠.请记住将其放置在实际使用的所有模板文件之后,例如在footer.php中,但是在管理栏渲染之前.
如果
admin-bar stuff
路径显示在顶部或任何其他文件中,请将此代码行中的文件名template-loader.php
更改为:您需要打破的filname.通常:class-wp-admin-bar.php
如果您需要在管理栏中进行此操作,请使用正确的优先级(最早)确保此列表末尾没有输入文件.例如:
add_action('admin_bar_menu', 'my_adminbar_template_monitor', -5);
优先级
-5
确保先加载.关键是在适当的时候调用get_included_files()
,否则需要弹出一些数组!要对此进行分解:
您不能在没有PHP回溯的情况下收集所有包含的模板文件.
template_include
内的 Superglobals 不会全部收集.另一种方法是在每个模板文件中"放置标记",但是如果您需要首先与这些文件进行交互,则会对时间和整个想法感到困惑.1)我们需要在内部检查当前Wordpress请求所使用的所有文件.他们很多!如果您在注册functions.php之前就使用了300个文件,请不要感到惊讶.
$included_files = str_replace('\\', '/', get_included_files());
我们正在使用PHP本机的get_included_files(),将反斜杠转换为正斜杠以匹配大多数Wordpress返回路径.
2)我们正在从template-loader.php的注册位置剪切该数组.之后,填充的get_included_files()应该只填充模板文件.
/* The magic point, we need to find its position in the array */ $path = str_replace('\\', '/', ABSPATH); $key = $path.'wp-includes/template-loader.php'; $offset = array_search($key, $included_files); /* Get rid of the magic point itself in the new created array */ $offset = ($offset + 1); $output = array_slice($included_files, $offset);
3)缩短结果,我们不需要路径,直到主题文件夹或插件文件夹,作为模板正在使用,可以混合来自插件,主题或子主题文件夹.
$replacement = $path.'wp-content/'; $output = str_replace($replacement, '', $output);
4)最后,从数组转换为漂亮的HTML列表
$output = '<ul><li>'.implode('</li><li>', $output).'</li></ul>';
如果您不要希望需要包含,则可能需要在第3部分中进行最后的修改 插件.他们可能会延迟调用
class-files
,并在模板输出处理过程中调用"拦截".但是,我发现让它们可见是合理的,因为这个想法是要跟踪加载的内容,即使在这个阶段渲染输出的不是"模板".
Here you go:
A HTML-list with all template files in use for the current landing page, including all template-parts from plugins, child theme and/ or parent theme combinations, all in one line of code:
echo '<ul><li>'.implode('</li><li>', str_replace(str_replace('\\', '/', ABSPATH).'wp-content/', '', array_slice(str_replace('\\', '/', get_included_files()), (array_search(str_replace('\\', '/', ABSPATH).'wp-includes/template-loader.php', str_replace('\\', '/', get_included_files())) + 1)))).'</li></ul>';
You MAY need to check that your server does not returning dubble slashes at any path. Remember to place this after all template files actually been used, like in footer.php, but before admin bar renders.
if
admin-bar stuff
path is showing at the top, or any other file, change the filenametemplate-loader.php
in this line of code to: whatever filname you need to break from. Often:class-wp-admin-bar.php
if you need this in the admin bar, use the right priotity (earliest) to make shure no files are entered at the end of this list. For example:
add_action('admin_bar_menu', 'my_adminbar_template_monitor', -5);
priority
-5
make shure it loads first. The key is to callget_included_files()
at the right moment, otherwise some array-popping needed!To break this up:
You can not collect all included template files without PHP backtrace. Superglobals inside
template_include
wont collect them all. The other way is to "place a marker" in each template file, but if you need to interact with the files first, you hazzle with time and the whole idea.1) We need to check inside all the files that have been used by current Wordpress request. And they are many! Dont be surprised if you are using 300 files before even your functions.php is registered.
$included_files = str_replace('\\', '/', get_included_files());
We are using the PHP native get_included_files(), converting backslashes to forward slashes to match most of Wordpress returning paths.
2) We are cutting that array from where the template-loader.php is registered. After that, the populated get_included_files() should only have template files populated.
/* The magic point, we need to find its position in the array */ $path = str_replace('\\', '/', ABSPATH); $key = $path.'wp-includes/template-loader.php'; $offset = array_search($key, $included_files); /* Get rid of the magic point itself in the new created array */ $offset = ($offset + 1); $output = array_slice($included_files, $offset);
3) Shorten down the results, we dont need the path until theme folder or plugin folder, as templates in use, can be mixed from plugins, theme or child theme folders.
$replacement = $path.'wp-content/'; $output = str_replace($replacement, '', $output);
4) Finally, convert from array to a nice HTML list
$output = '<ul><li>'.implode('</li><li>', $output).'</li></ul>';
A last modification might be needed in part3) -replacement, if you dont want required includes by plugins. They might call
class-files
late, and "intercept" during the template output processing.However, I found it reasonable to leave them visible, as the idea is to track whats been loaded, even if it is not a "template" that rendering output in this stage.
-
- 2011-12-25
我发现的最简单的方法是在body标签上包括WordPress函数.它将根据您正在查看的页面(首页显示首页,页面显示页面等)添加几个类.
在此处查看: http://codex.wordpress.org/Function_Reference/body_class
此外,对于在这些页面上使用CSS定位元素很有帮助.
David R提到的了解模板层次结构(http://codex.wordpress.org/Template_Hierarchy)也是一个好主意.
Easiest way I've found is to include the WordPress function on the body tag. It'll add several classes depending on which page you're viewing (home for the front, page for page, etc).
Check it out here: http://codex.wordpress.org/Function_Reference/body_class
Plus it's helpful for targeting elements with CSS on those pages.
Getting to know the Template Hierarchy (http://codex.wordpress.org/Template_Hierarchy) as David R mentioned is also a good idea.
-
- 2013-01-29
还有另一个专门用于此目的的准系统插件.我倾向于安装调试栏,因为这些其他功能看起来很有用,但这是更基本的功能,专门用于此目的: http://wordpress.org/extend/plugins/what-the-file/
There's another more bare-bones plugin specifically for this purpose. I'm leaning towards installing the debug bar, because those other features look useful, but this one is more basic and specifically for this purpose: http://wordpress.org/extend/plugins/what-the-file/
-
- 2011-12-24
我做的一件非常简单的事情是在主题的每个相关文件中插入HTML注释,以标识模板文件,例如在我拥有的index.php顶部
<!-- index -->
和位于front-page.php顶部
<!-- front -->
但是很明显,这需要修改主题.我怀疑您可以在footer.php文件或header.php文件中添加自定义函数,该函数将告诉您正在使用的文件.我倾向于使用上述方法和参考图表 http://codex.wordpress.org/Template_Hierarchy 使用.
One very simple thing I do is to insert an HTML comment identifying the template file in each relevant file of the theme, eg at the top of index.php I have
<!-- index -->
and at the top of front-page.php
<!-- front -->
But obviously that requires modifying the theme. I suspect you could add a custom function in the footer.php file or header.php which would tell you what file was being used. The above method and the reference chart http://codex.wordpress.org/Template_Hierarchy are what I tend to use.
-
- 2011-12-26
有一个名为主题检查的插件,该插件正是这样做的.它显示当前用作HTML注释的模板文件的名称.
There is a plugin named Theme Check which does exactly this. It displays the name of the current template file in use as a HTML comment.
激活wordpress主题时,找出要更改文件的文件总是很麻烦. 任何想法如何简化事情?
但是,另一方面,考虑到get_template_part功能,这可能是不可能的.你怎么说?