get_header操作不起作用

问题描述:

我正在尝试使用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

下一个解决方案不喜欢我,但是是一个选项:

去 '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'); 

我从来没有修改源代码,我只把这里一个解决问题的办法。

如果您已经创建了一个名为header-newtmpl.php的不同头文件模板,您只需传递一个参数即可获取新头文件。

get_header();

将其更改为get_header('newtmpl');

其中newtmpl是新的报头的名称,它将获取的header-newtmpl.php代替header.php