问题描述

我已经接管了一个大型 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 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。