dongzhan2029
2013-11-02 20:20
浏览 81
已采纳

有没有办法知道哪个PHP文件在Wordpress中生成了一个页面?

I am trying to make some changes to PHP files in Wordpress, but it is taking me a long time to find which PHP file to edit. Is there a way to know which PHP file generated a given page? Thanks!

More information: I understand the basic outline of Wordpress templates like header.php and single.php. However, I am having a hard time walking through the many theme-specific template files and finding which one serves what purpose. Specifically, I am looking at a generated webpage and attempting to edit it. And I am resorting to inserting

tags inside each of the probable template files until I find the right one. Is there a way, perhaps through dev-tool, to see which php file generated the DOM?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试对Wordpress中的PHP文件进行一些更改,但是我花了很长时间才找到哪个 要编辑的PHP文件。 有没有办法知道哪个PHP文件生成了给定的页面? 谢谢!

更多信息: 我理解Wordpress模板的基本概要,如header.php和single.php。 但是,我很难浏览许多特定于主题的模板文件,并找出哪一个用于什么目的。 具体来说,我正在查看生成的网页并尝试编辑它。 我想在每个可能的模板文件中插入

标签,直到找到合适的标签。 有没有办法,也许通过开发工具,看看哪个php文件生成了DOM?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doudi7782 2013-11-03 00:04
    已采纳

    It is possible to get a list of all the included files through the get_included_files() function in PHP. Here is a PHP script to set in the footer.php file of your template :

    // echo '<!--'; // you can display this code or show it in an HTML comment
    $files = get_included_files();
    if ($display_only_theme_files === true) {
      $theme_folder = get_template_directory();
      foreach ($files as $key => $file) {
        if (strstr($file, $theme_folder) === false) {
          unset($files[$key]);
        }
      }
    }
    print_r($files);
    // echo '-->'; // you can display this code or show it in an HTML comment
    

    This script will show you all the included files related to your template. In other words, you will have the possibility to know which file template is used.

    Please, be sure to use this code only on your development mode and to delete it when you will be in production.

    点赞 打赏 评论
  • dongtuan5367 2013-11-02 20:55

    It is sometimes difficult trying to work out which template file within a theme is being used. First thing worth considering is the Wordpress template hierarchy. This page from the Codex and in particular the diagram should be helpful to you;

    http://codex.wordpress.org/Template_Hierarchy

    Secondly, it can help if you add the body_class method to your theme's header.php. It is designed to allow greater control of CSS through additional specificity, but by viewing the source through your browser dev tools you can quickly look at the classes added to the body tag and work out which template is being used;

    http://codex.wordpress.org/Function_Reference/body_class

    点赞 打赏 评论
  • duanke2503 2014-09-03 14:33

    How about putting a unique HTML comment in each template file?

    点赞 打赏 评论

相关推荐 更多相似问题