
本文详解如何让 WordPress 的 the_custom_logo() 函数在首页也保持可点击并跳转至首页,解决因主题启用 'unlink-homepage-logo' => true 导致的链接被移除问题。
本文详解如何让 wordpress 的 `the_custom_logo()` 函数在首页也保持可点击并跳转至首页,解决因主题启用 `'unlink-homepage-logo' => true` 导致的链接被移除问题。
在基于 Twenty Twenty-One 或其子主题(如 twentytwentyone-child)开发时,你可能会发现:尽管调用了 the_custom_logo(),但当用户访问网站首页时,Logo 图片不再包裹 <a></a> 标签,即无法点击返回首页——而这是不符合多数用户体验预期的行为。
这并非模板代码(如 site-branding.php)遗漏所致,而是由主题在 functions.php 中通过 add_theme_support('custom-logo', ...) 显式启用了 'unlink-homepage-logo' => true 选项所导致。该参数是 WordPress 5.5+ 引入的特性,意在允许主题“在首页自动解绑 Logo 链接”,但默认行为(即始终链接到首页)更常见且更合理。
✅ 正确修复方式:重置自定义 Logo 支持参数
你不能直接在子主题的 functions.php 顶层作用域中调用 remove_theme_support() 和 add_theme_support() —— 因为父主题的 add_theme_support() 通常在 after_setup_theme 钩子中执行,若子主题代码未挂载到对应时机,将失效。
✅ 必须使用 after_setup_theme 钩子,并设置比父主题更低的优先级(例如 9),确保在父主题注册之后再覆盖:
// 在子主题的 functions.php 中添加以下代码
function child_theme_setup_custom_logo() {
// 先移除父主题已注册的 custom-logo 支持
remove_theme_support('custom-logo');
// 重新注册,关键:设置 'unlink-homepage-logo' => false
$logo_width = 300;
$logo_height = 100;
add_theme_support('custom-logo', array(
'height' => $logo_height,
'width' => $logo_width,
'flex-width' => true,
'flex-height' => true,
'unlink-homepage-logo' => false, // ? 核心修复项:强制首页保留链接
));
}
add_action('after_setup_theme', 'child_theme_setup_custom_logo', 9);⚠️ 注意事项:
add_action的优先级设为9(父主题通常为10),确保执行顺序正确;$logo_width/$logo_height值建议与父主题一致(可查阅父主题functions.php中原始定义),避免影响裁剪逻辑;- 修改后请清空对象缓存(如有)并刷新前台首页验证效果;
- 无需修改任何模板文件(如
site-branding.php),the_custom_logo()将自动按新配置渲染带链接的 Logo。
? 验证是否生效
在首页查看源码,确认 Logo 输出类似如下结构(含 <a href="https://yoursite.com/"></a>):
<div class="site-logo">
<a href="https://yoursite.com/" class="custom-logo-link" rel="home">
<img style="max-width:90%" style="max-width:90%" src="https://yoursite.com/wp-content/uploads/logo.png" class="custom-logo" alt="Site Title">
</a>
</div>至此,无论用户处于首页还是内页,点击 Logo 均可一致跳转至网站首页,实现专业、统一的导航体验。


















