我可以仅使用自定义插件覆盖我的自定义块主题模板(即index.html、404.html)吗?

问题描述 投票:0回答:1

我开始开发一个块主题,它将具有相同的主结构但不同的内容,最终结果也可以通过覆盖模板部分来实现。

因此,我正在尝试找到一种方法来仅从插件代码中覆盖模板或模板部分,如果我必须更改插件,则无需更改主题。

我尝试过

add_filter( 'page_template' )
甚至没有触发,
add_filter( 'theme_page_templates' )
也没有触发。

我查看了 WordPress Codex 是否有任何可以帮助我的钩子,但没有找到。

有什么想法吗?

php wordpress wordpress-theming wordpress-gutenberg
1个回答
0
投票

我相信 template_include 过滤器就是您正在寻找的。

add_filter( 'template_include', 'custom_404_template', 99 );
function custom_404_template( $template ) {
    if ( is_404()  ) {
        $new_template = dirname( __FILE__ ) . '/templates/your_404_page.php';
        return $new_template;
    }
    return $template;
}
© www.soinside.com 2019 - 2024. All rights reserved.