Page.Title same as Site.Title on Hugo Site

Viewed 2234

I am building a website with Hugo. In the config.toml page there is:

title = "mySite"

And in the markdown file for the About page, there is:

title: About

This works and results in a browser tab that says, About - mySite.

The problem arises on the home page where the browser tab says, mySite - mySite.

The markdown home page does define title:

title: long name of post

Browsing the Hugo files, I see that head.html has the line:

<title>{{ if .Page.Title }}{{ .Page.Title }} - {{ end }}{{ .Site.Title }}</title>

It appears that .Page.Title = .Site.Title for the home page. How can I set the home page .Page.Title to be "Home" so that the browser tab of the home page reads, "Home - mySite"?

1 Answers

Try looking into .IsHome page variable (https://gohugo.io/variables/page/).

For the case you are describing, I think this could do the trick:

<title>{{ if .IsHome }}Home - {{ else }}{{ if .Page.Title }}{{ .Page.Title }} - {{ end }}{{ end }}{{ .Site.Title }}</title>

I didn't have a handy site to quickly test the above template rendering, but make sure there is no space between curly braces and Home i.e. }}Home, otherwise the space will appear in the <title> as well. If there is syntax error above, just write in the comment and I'll edit the code.

Related