Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
715 views
Welcome To Ask or Share your Answers For Others

1 Answer

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:

output template name to content


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...