如何重命名模板文件?

时间:2012-07-05 作者:Doug Cassidy

如果我有指向somefilename.php 并希望将文件名更改为betterfilename.php, 我可以保持模板注释不变而不手动更改所有页面吗?

/*
Template Name: Some File Name
*/

2 个回复
最合适的回答,由SO网友:Doug Cassidy 整理而成

Chris给了我一些很好的见解,我很欣赏filter func。但我最终通过phpMyAdmin更改了db:

UPDATE wp_postmeta SET meta_value = \'new-filename.php\' WHERE meta_value = \'old-filename.php\';

SO网友:chrisguitarguy

当您选择页面模板时,WordPress会使用键将其存储为Posteta_wp_page_template. 存储的值是相对于模板目录(或样式表目录)的实际页面模板路径。

因此,当WP查找要包含的模板时,它将somefilename.php 当它找不到它时,它会回到page.php.

您可以在函数中看到WP搜索的文件get_page_templateget_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.

结束

相关推荐

Enable page templates. How?

基本问题,但我想启用页面模板。我有一个启用了页面模板的主题。我切换到了另一个模板,但没有更改模板的选项,即使在创建新页面时也是如此。如何打开此选项?我在抄本和论坛上找到了根,但找不到。