You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过functions.php移除WordPress Twenty Seventeen主题的custom-header

移除Twenty Seventeen主题的custom-header区域(通过functions.php)

你之前的代码无效,核心原因是钩子执行顺序和优先级问题:子主题的functions.php加载早于父主题,当你的remove_custom_header函数以默认优先级10执行时,父主题的twentyseventeen_custom_header_setup还没被添加到after_setup_theme钩子上,自然无法移除。

下面是几个可行的解决方案:

方案一:调整优先级,确保移除动作在父主题钩子之后执行

给你的移除函数设置更高的优先级(比如20,比默认的10大),让它在父主题的钩子注册完成后再执行:

function remove_custom_header() {
    remove_action('after_setup_theme', 'twentyseventeen_custom_header_setup');
}
add_action('after_setup_theme', 'remove_custom_header', 20);

方案二:直接取消主题对custom-header的支持

twentyseventeen_custom_header_setup的核心作用是注册custom-header的主题支持,你可以直接移除该支持:

function remove_custom_header_support() {
    remove_theme_support( 'custom-header' );
}
add_action('after_setup_theme', 'remove_custom_header_support', 20);

方案三:移除header区域的图片输出动作

如果上面的方法仍有残留,可以直接移除负责显示header图片的钩子动作:

function remove_header_image_display() {
    remove_action('twentyseventeen_header', 'twentyseventeen_header_image', 8);
}
add_action('init', 'remove_header_image_display');

注意事项

  • 操作后务必清除WP-Optimize的缓存、预加载文件,同时清空浏览器缓存再测试。
  • 所有代码都要添加到子主题的functions.php中,不要直接修改父主题文件,避免主题更新丢失修改。

内容的提问来源于stack exchange,提问作者Francisco Romero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 08:37:23