当您选择页面模板时,WordPress会使用键将其存储为Posteta_wp_page_template
. 存储的值是相对于模板目录(或样式表目录)的实际页面模板路径。
因此,当WP查找要包含的模板时,它将somefilename.php
当它找不到它时,它会回到page.php
.
您可以在函数中看到WP搜索的文件get_page_template
和get_page_template_slug
<?php
// in wp-includes/template.php
/**
* Retrieve path of page template in current or parent template.
*
* Will first look for the specifically assigned page template
* The will search for \'page-{slug}.php\' followed by \'page-id.php\'
* and finally \'page.php\'
*
* @since 1.5.0
*
* @return string
*/
function get_page_template() {
$id = get_queried_object_id();
$template = get_page_template_slug();
$pagename = get_query_var(\'pagename\');
if ( ! $pagename && $id ) {
// If a static page is set as the front page, $pagename will not be set. Retrieve it from the queried object
$post = get_queried_object();
$pagename = $post->post_name;
}
$templates = array();
if ( $template && 0 === validate_file( $template ) )
$templates[] = $template;
if ( $pagename )
$templates[] = "page-$pagename.php";
if ( $id )
$templates[] = "page-$id.php";
$templates[] = \'page.php\';
return get_query_template( \'page\', $templates );
}
// in wp-includes/post-template.php
/**
* Get the specific template name for a page.
*
* @since 3.4.0
*
* @param int $id The page ID to check. Defaults to the current post, when used in the loop.
* @return string|bool Page template filename. Returns an empty string when the default page template
* is in use. Returns false if the post is not a page.
*/
function get_page_template_slug( $post_id = null ) {
$post = get_post( $post_id );
if ( \'page\' != $post->post_type )
return false;
$template = get_post_meta( $post->ID, \'_wp_page_template\', true );
if ( ! $template || \'default\' == $template )
return \'\';
return $template;
}
最佳解决方案:去更新你的页面。其他较少手动的解决方案:挂钩
page_template
并查找旧文件名,将其替换为新文件名:
<?php
add_filter(\'page_template\', \'wpse57568_page_template\');
function wpse57568_page_template($t)
{
$old_slug = \'pages/t.php\'; // replace this
$new_slug = \'pages/new.php\'; // replace this
$page_id = get_queried_object_id();
$template = get_post_meta($page_id, \'_wp_page_template\', true);
if($template && \'default\'!= $template && $old_slug == $template)
{
if(file_exists(trailingslashit(STYLESHEETPATH) . $new_slug))
{
$t = trailingslashit(STYLESHEETPATH) . $new_slug;
}
elseif(file_exists(trailingslashit(TEMPLATEPATH) . $new_slug))
{
$t = trailingslashit(TEMPLATEPATH) . $new_slug;
}
}
return $t;
}
作为
plugin.