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

Hugo:为特定页面覆盖主题的标题生成逻辑

Fixing Hugo's humanize Removing Hyphens in Page Titles (Per-Page or Global)

Great question! The humanize function in Hugo is built to convert hyphens, underscores, and other separators into spaces while normalizing capitalization—exactly why your "A-Foobar etc etc" title is losing its hyphen. Here are practical solutions to resolve this, including options to target only specific pages:

Option 1: Use a Custom Front Matter Parameter (Most Flexible)

This lets you define a custom title for specific pages that bypasses the humanize filter entirely, while keeping the original logic for all other pages.

Step 1: Add a Custom Parameter to Your Page's Front Matter

In the markdown file of the page with the hyphenated title, add a custom_title field:

---
title: "A-Foobar etc etc"
custom_title: "A-Foobar etc etc" # Match your desired title exactly
---

Step 2: Modify the Theme's metadata.html Partial

Update the title logic in layouts/partials/head/metadata.html to check for this custom parameter first:

<title>
  {{ if .Params.custom_title }}
    {{ .Params.custom_title | title }} <!-- Use the custom title directly -->
  {{ else }}
    {{ .Title | humanize | title }} <!-- Original logic for other pages -->
  {{ end }}
  {{ if ne .Title .Site.Title }} - {{ .Site.Title | markdownify }}{{ end }}
</title>

This way, any page with custom_title will retain its hyphens (or any other formatting you want), while all other pages continue using the theme's original title processing.

Option 2: Target Specific Pages by Slug/URL (No Front Matter Changes)

If you only need to fix one or two pages and don't want to add front matter parameters, you can conditionally skip humanize based on the page's slug or URL.

Modify the metadata.html partial like this:

<title>
  {{ if eq .Slug "a-foobar-etc-etc" }} <!-- Replace with your page's slug -->
    {{ .Title | title }} <!-- Use the raw title without humanize -->
  {{ else }}
    {{ .Title | humanize | title }} <!-- Original logic for all other pages -->
  {{ end }}
  {{ if ne .Title .Site.Title }} - {{ .Site.Title | markdownify }}{{ end }}
</title>

To find your page's slug: check the URL (it's usually the filename without .md), or run hugo server and inspect the page's .Slug value via Hugo's debug tools.

Option 3: Avoid humanize Entirely (Global Change)

If you want to disable humanize for all pages (not just specific ones), simply remove the humanize filter from the title logic:

<title>
  {{ .Title | title }}
  {{ if ne .Title .Site.Title }} - {{ .Site.Title | markdownify }}{{ end }}
</title>

Note that this will affect all pages—any titles that relied on humanize to fix formatting (like underscores to spaces) will now retain their original separators.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 16:52:37