从管理页面查找用于页面的Wordpress模板

kap*_*oko 12 php wordpress templates admin

我正在尝试检索仪表板中"编辑页面"页面上使用的模板的文件名/路径.

类似于wp-includes/template-loader.php()在前端的作用:找出要渲染的模板.

不幸的是,像is_front_page()Wordpress' template-loader.php用来查明它是否应该使用的表达式- get_front_page_template()在管理页面上无法正常工作.这是预期的,因为这些表达式使用全局$ wp_query对象,而不是当前查询.

到目前为止我尝试过的:

在管理页面中运行post循环

$args = array(
    'p' => get_the_ID(),
    'post_type' => 'any'
);

$query = new \WP_Query($args);

if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

    <?= the_title(); ?><br>
    Is front page: <?= is_front_page() ? 'true' : 'false' ?>

<?php endwhile; endif; ?>
Run Code Online (Sandbox Code Playgroud)

显示:

是头版:假

使用get_post_meta

<?= get_post_meta(get_the_ID(), '_wp_page_template', true); ?>
Run Code Online (Sandbox Code Playgroud)

显示:

默认

...对于Home上的front-page.php和另一个默认页面上的page.php将是相同的,所以这对我没有帮助.

简而言之

我想要的是front-page.php在我编辑"主页"页面时.或者custom-template.php当我在选择自定义模板的情况下编辑某个页面时.或者about-page.php当我正在编辑一个名为"关于"的页面时.如何获取正确的文件名或路径?

Spa*_*cus 2

使用get_page_template()

<?php echo realpath(get_page_template()); ?> 
Run Code Online (Sandbox Code Playgroud)

它输出类似的东西/foo/bar/baz/wp-content/themes/your-theme/{page-template}.php

您可以选择不使用realpath()而仅获取模板名称。