問題描述

我正在使用 TwentyTen 主題來建立一個小孩主題,但我似乎無法擺脫 TwentyTen 父主題中的 「One column,no sidebar」 頁面模板。

我認為只是複製它,刪除內容會做的伎倆,但似乎沒有。有誰知道如何做到這一點?我確定這很簡單。

謝謝

OSU

最佳解決方案

覆蓋該模板會比擺脫它更容易。只是邏輯的方式。

我沒有聲稱它是高效的想法 (在這裡晚),但這將得到它從編輯螢幕:

add_action('admin_head-post.php','remove_template');

function remove_template() {

    global $wp_themes;

    get_themes();
    $templates = &$wp_themes['Twenty Ten']['Template Files'];
    $template = trailingslashit( TEMPLATEPATH ).'onecolumn-page.php';
    $key = array_search($template, $templates);
    unset( $templates[$key] );
}

次佳解決方案

WordPress 3.9 引入了 theme_page_templates 濾鏡。

來自 Twenty Fourteen 子主題 functions.php 的示例顯示如何刪除”Contributor Page” 模板:

function tfc_remove_page_templates( $templates ) {
    unset( $templates['page-templates/contributors.php'] );
    return $templates;
}
add_filter( 'theme_page_templates', 'tfc_remove_page_templates' );

第三種解決方案

擴充套件 @ Rarst 的答案,這是一個更通用的方法,並不是繫結到一個特定的主題,而是可以在你自己的孩子主題的 functions.php 中使用,以打破你想要擺脫的任何父主題頁面模板。

function remove_template( $files_to_delete = array() ){
    global $wp_themes;

    // As convenience, allow a single value to be used as a scalar without wrapping it in a useless array()
    if ( is_scalar( $files_to_delete ) ) $files_to_delete = array( $files_to_delete );

    // remove TLA if it was provided
    $files_to_delete = preg_replace( "/.[^.]+$/", '', $files_to_delete );

    // Populate the global $wp_themes array
    get_themes();

    $current_theme_name = get_current_theme();

    // Note that we're taking a reference to $wp_themes so we can modify it in-place
    $template_files = &$wp_themes[$current_theme_name]['Template Files'];

    foreach ( $template_files as $file_path ){
        foreach( $files_to_delete as $file_name ){
            if ( preg_match( '//'.$file_name.'.[^.]+$/', $file_path ) ){
                $key = array_search( $file_path, $template_files );
                if ( $key ) unset ( $template_files[$key] );
            }
        }
    }
}

所以你可以在你的小孩主題的 functions.php 檔案中使用它:

add_action( 'admin_head-post.php', 'remove_parent_templates' );

function remove_parent_templates() {
    remove_template( array( "showcase.php", "sidebar-page" ) );
}

這裡我只是說明你不必透過”.php” 部分,如果你不想。

或者:remove_template( "sidebar-page" ); – 如果要僅修改單個檔案,則不需要傳遞陣列。

第四種方案

WP 核心 (3.9) 中有一個新的過濾器來刪除頁面模板。它可以從兒童主題中使用。

這是如何在 TwentyTen 中實現的 (在 WP 3.9 中測試):

add_filter( 'theme_page_templates', 'my_remove_page_template' );
    function my_remove_page_template( $pages_templates ) {
    unset( $pages_templates['onecolumn-page.php'] );
    return $pages_templates;
}

https://core.trac.wordpress.org/changeset/27297

http://boiteaweb.fr/theme_page_templates-hook-semaine-16-8033.html

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。