如何从WordPress中的子主题扩展类?

我正在尝试扩展父主题中的类, 但是当我尝试将类添加到functions.php文件中时出现致命错误, 则表示未找到父类。我的类是这样的:

class MyClasss extends ParentTheme_Templates {
public static function header() {
    $type = ParentTheme_Global::instance()->get_header_type();

    get_template_part( 'mycomponents/headers/header', $type );
}

但是我可以在我的模板文件(我在header.php文件中尝试过)中为类使用相同的代码, 并且可以工作, 但这不是我认为的正确方法。因此, 有没有适当的方法可以做到这一点。我可以在法典中看到:

与style.css不同, 子主题的functions.php不会覆盖其父主题中的对应对象。相反, 除了父项的functions.php之外, 还会加载它。 (具体来说, 它是在父文件之前加载的。)

有什么办法可以解决这个问题?


#1


子主题在父主题之前加载, 因此你需要在挂钩中定义该类, 该挂钩在父主题加载之后运行, 这样就可以扩展父主题类。 after_setup_theme将是合理的选择。

function mytheme_includes() {
    require_once get_theme_file_path( 'includes/class-my-class.php' );
}
add_action( 'after_setup_theme', 'mytheme_includes' );

谢谢@jakept

来源:

https://www.srcmini02.com/63298.html

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?