通过functions.php替换标头的一部分

时间:2019-04-02 11:14:40

标签: wordpress function genesis

我正在尝试通过functions.php中的代码在Wordpress Genesis框架中自定义header.php的部分:

if ( class_exists( 'page-template' ) ) {
    remove_action( 'genesis_header', 'genesis_do_header' );
    add_action( 'genesis_header', 'gd_genesis_do_header' );

    function gd_genesis_do_header() {
        echo 'test';
    }
}

目标是使修改内容显示在具有特定正文类“页面模板”的页面上。

该函数在没有'if'语句的情况下起作用,但没有。这有什么问题吗?

或者也许可以基于页面执行操作?

2 个答案:

答案 0 :(得分:1)

代替检查正文中的page-template类,只需检查页面模板是否在页面上正在使用,就像这样:

if ( is_page_template() ) {
    remove_action( 'genesis_header', 'genesis_do_header' );
    add_action( 'genesis_header', 'gd_genesis_do_header' );

    function gd_genesis_do_header() {
        echo 'test';
    }
}

这里是more information about WordPress' is_page_template() function。您甚至可以使用该功能来检查是否使用了 specific 页面模板。例如,如果正在使用is_page_template( 'about.php' )模板,则true将返回about.php

答案 1 :(得分:0)

要检查主体类,请使用get_body_class NOT class_exists来检查OOP中使用的PHP类。

if ( in_array( 'page-template', get_body_class() ) ) {