How to find out which WordPress template is using on a website page?
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'; ?>
Have any easier ways?
5
1 answers
The global variable $template
contains this information. We can put a filter on the_content
to print this and make it only appear 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
from child theme
Seeing a simple post: child theme does not have single.php
, site using parent theme file
Another option is to print this information as an HTML comment on the <head>
:
add_action( 'wp_head', 'sopt_print_template_in_head', 999 );
function sopt_print_template_in_head() {
global $template;
echo '
<!--
TEMPLATE = ' . $template . '
-->
';
}
1
Author: brasofilo, 2017-05-17 06:50:14