How can I find out which template WordPress is using on a site page?

4

I'm often not sure exactly which theme file is generating a site page, for example, http://example.com/nome-da-pagina-post .

What I do is put a echo inside each theme template, single.php , archive.php ...

<?php
/**
 * The Template for displaying all single posts.
 *
 * @package WordPress
 * @subpackage Twenty_Thirteen
 * @since Twenty Thirteen 1.0
 */

get_header();
echo 'SINGLE.PHP'; ?>

Is there any easier way?

    
asked by anonymous 17.05.2017 / 08:50

1 answer

1

The global variable $template contains this information. We can put a filter on the_content to print this and make it appear only to the admin:

add_filter( 'the_content', 'sopt_print_template', 20 );

function sopt_print_template( $content ) {
    # O valor da global $template vai ser tipo:
    # /public_html/wp-content/themes/theme-name/template-name.php
    global $template;

    # Não executar o filtro se estivermos no backend 
    # ou se o usuário não for um administrador
    if( is_admin() || !current_user_can( 'delete_plugins' ) )
        return $content;

    # Buscar o nome da pasta e do arquivo
    $split_path = explode( '/', $template );
    $total = count( $split_path ) - 1;
    $theme_and_template = $split_path[$total-1] . '/' . $split_path[$total];
    $print = '<strong style="font-size:1em;background-color:#FFFDBC;padding:8px">Current = ' . $theme_and_template . '</strong>';

    # Adicionar a informação antes do conteúdo
    $content = $print . $content;
    return $content;
}

Site home page: using child theme%

  

Viewingasimplepost:childthemedoesnothasindex.php,siteusingparenttheme

  

Another option is to print this information as an HTML comment on single.php :

add_action( 'wp_head', 'sopt_print_template_in_head', 999 );    

function sopt_print_template_in_head() {
    global $template;
    echo '
    <!--

    TEMPLATE = ' . $template . '

    -->
    ';
}
    
17.05.2017 / 08:50