問題描述
我已經接管了一個大型 WP-MS 網站的維護。該網站有〜 200 個模板,許多頁面特定,沒有標準的命名過程。
如果在網站上衝浪時可以節省大量的時間,我可以看到構成當前頁面的模板名稱。這可能嗎?我查看了 WordPress 的調試插件,但它們看起來面向變量和 SQL 查詢,而不是模板。
最佳解決方案
這也是一個快速的方式;
<!--
<?php print_r( debug_backtrace() ) ?>
-->
粘貼在結束標籤之前
次佳解決方案
以下功能有三件事情:
-
顯示當前需求的模板層次結構
-
顯示使用的主題 (2 種方式來顯示這個)
-
顯示正在使用的請求模板*)
*) 將其附加到內容過濾器。您可能需要根據您的角色玩條件或功能。到目前為止,我不知道一個解決方案來顯示歸檔和類似列表視圖請求的頁面模板。
// That's not that easy to read:
var_dump( get_required_files() );
/**
* Show template hierarchy and theme at the end of the request/page.
* @return void
*/
function wpse31909_template_info()
{
// Don't display for non-admin users
if ( ! current_user_can( 'manage_options' ) )
return;
// You have to build yourself the hierarchy here or somewhere in front of the fn
global $wp_template_hierarchy;
$content = '<pre>';
// Show template hierarchy
$content .= "TEMPLATE HIERARCHYn==================n";
$content .= var_export( $wp_template_hierarchy, true );
// Show current theme in use:
$content .= "nnCURRENT THEMEn=============n";
$content .= var_export( get_option( 'template' ), true );
// or:
# $content .= var_export( get_template(), true );
$content .= '</pre>';
return print $content;
}
add_action( 'shutdown', 'wpse31909_template_info' );
/**
* Show template on singular views attached to the end of the content for admins
* @return $content
*/
function wpse31909_template_to_content( $content )
{
// Display standard content for non-admin users and not single post/page/cpt/attachment view.
if ( ! current_user_can( 'manage_options' ) && ! is_singular() )
return $content;
$content .= '<pre>';
// Show current template in use: Must be in the loop to get the global $post
$content .= var_export( get_post_meta( $GLOBALS['post']->ID, '_wp_page_template' ), true );
$content .= '</pre>';
return $content;
}
add_filter( 'the_content', 'wpse31909_template_to_content' );
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。