How do I get the name of the file that is being used to render the current page?

a 夏天 提交于 2019-11-29 13:52:06

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

Which results in:

As far as I've seen there's no built in option to enable such logging, only for errors.

I'm not sure what editor you use, but most common text editors allow you to do a find replace across an entire folder.

I'd suggest doing a temporary replace on includes and require's to add an echo of the PHP_SELF. Just make sure to add a comment or something before the echo so that you can easily replace them with nothing when you're done.

A quick search on the WordPress plugin repository brings up a WordPress Debug Bar Template Trace.

I just manually type it into the template, i.e. ARCHIVE.PHP, CATEGORY-1.PHP when I'm building it. Just remember to delete it once the site goes live. Simple and easy, if not so graceful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!