2016-09-23 251 views
2

我正在嘗試使用get_header操作更改標題模板,但它無法改變,我嘗試去做。get_header操作不起作用

這是我試圖通過在functions.php中添加此:

function prefix_new_header() { 
    return 'newtmpl'; 
} 
add_action('get_header', 'prefix_new_header'); 

我在主題文件夾一個文件名爲頭,newtmpl.php

我試過不同的優先級(1,10,99),但它不起作用。

有什麼我失蹤?謝謝你的幫助!

如果你想測試它,你可以在第二十六次嘗試這個。

+0

的行動掛鉤不返回的內容,返回是過濾器鉤子。也許你修改header.php模板。這個問題很有趣!我也需要這個! –

+0

我在所有需要它的模板(如index.php,single.php等等)中使用'get_header()',而沒有任何修改。 – superwinner

回答

0

下一個解決方案不喜歡我,但是是一個選項:

去 'WP-包括/一般的template.php' 文件,並添加:

$templates = apply_filters('replace_header', $templates); 

在get_header功能最終的結果功能是:

function get_header($name = null) { 
    /** 
    * Fires before the header template file is loaded. 
    * 
    * The hook allows a specific header template file to be used in place of the 
    * default header template file. If your file is called header-new.php, 
    * you would specify the filename in the hook as get_header('new'). 
    * 
    * @since 2.1.0 
    * @since 2.8.0 $name parameter added. 
    * 
    * @param string $name Name of the specific header file to use. 
    */ 
    do_action('get_header', $name); 

    $templates = array(); 
    $name = (string) $name; 
    if ('' !== $name) { 
     $templates[] = "header-{$name}.php"; 
    } 

    $templates[] = 'header.php'; 

    $templates = apply_filters('replace_header', $templates); //This is the new line. 

    locate_template($templates, true); 
} 

而在這之前,你可以取代所有模板:

function prefix_new_header($templates) { 
    return 'header-newtmpl.php'; 
} 
add_filter('get_header', 'prefix_new_header'); 

或只加你:

function prefix_new_header($templates) { 
    $templates[] = 'header-newtmpl.php'; 
    return $templates; 
} 
add_filter('get_header', 'prefix_new_header'); 

我從來沒有修改源代碼,我只把這裏一個解決問題的辦法。

0

如果您已經創建了一個名爲header-newtmpl.php的不同頭文件模板,您只需傳遞一個參數即可獲取新頭文件。

get_header();

將其更改爲get_header('newtmpl');

其中newtmpl是新的報頭的名稱,它將獲取的header-newtmpl.php代替header.php