How to find out which template Wordpress is using on a site page?

Asked

Viewed 341 times

5

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

What I do is I put one echo within 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 an easier way?

  • 2

    Good to see you here again!

  • 2

    salve salve salve, Sergio :)

  • 3

    Long live the brasofilo!!!!!!!!!!!!!!!!!!

  • 2

    hey, @big, Gracias!

1 answer

1

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

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

Main page of the site: using index.php of Child Theme

child theme index.php

Seeing a simple post: the Child Theme nay has single.php, website using the Parent Theme file

parent theme single.php


Another option is to print this information as an HTML comment on <head>:

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

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

    TEMPLATE = ' . $template . '

    -->
    ';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.