Is there a better way to render the Title of a parent Section or Taxonomy in children Pages?

This works but wonder if there might be a better way. If I am at a content page, I want to get the title of the section that page is in. This will become part of the in the html section.

<head>

    ...more stuff...

    {{ $toptitle := .Site.Title }}
    {{ if .IsPage }}
        {{ $sectitle := (.Site.GetPage "section" .Section).Title }}
        {{ $toptitle = $sectitle }}
    {{ end}}
    {{ $title := print .Title " | " $toptitle }}
    {{ if .IsHome }}{{ $title = .Site.Title }}{{ end }}
    <title>{{ $title }}</title>

    ...more stuff

</head>

Why? This blog at a-view.org is a collection of travelogues using the Casper3 theme. I wanted to see if I could marginally improve the SEO. For example, before the change above, the meta title of a page in our bike ride across America appeared as: “We did it! | Trips & Places”. A search engine wouldn’t know this page had anything to do with a bike ride across America. With this change the the meta title appear as: “We Did It! | Bike Across America”.

This is not exactly a high volume site, but I was disappointed that when I “googled” bike ride across America, my lovely little site didn’t come up. I actually had to google the exact title of a page (which I am certainly the only human on the planet to know) and lo, the page was the top of the web results. Hoo-hoo! I have no great hopes for this and I am not really trying to trick Google (as if that would work). Just trying to provide a more relevant shred of text that Google considers when indexing pages.

The logic is a bit screwy as written but it handles all kinds of pages. In particular, it needs to handle taxonomy pages. For example, when we are at a section (which lists the pages in the section as postcards) the title should be “Section | Trips & Places”. If we are on an author page the title should be “George Smith | Trips & Places”. And likewise when I get around to using other taxonomies.

This project started as separate Wordpress blogs. Then, separate Ghost blogs. Then, one combined Ghost blog with one site menu item pointing to a Hugo blog. Now, it’s all one Hugo blog (I could make them separate, but they share a theme, authors, and eventually some categories like “restaurants”, “things to see”, etc.) with one menu item pointing to static pages generated from a Wordpress blog that was too much work to convert. It all works and works best in Hugo.

The mysteries of the Google algorithm are beyond the scope of this forum, but let me tell you that you’re most certainly not the only one seeing this behaviour.

However we cannot offer advice other than what is already available in third party literature regarding SERP optimisation etc. So please look it up in other channels as we cannot have this discussion here.


Actually what you’re already using is an interesting method i.e. $sectitle := (.Site.GetPage "section" .Section).Title.

However the truth is that to achieve what you ask site-wide requires a bit of work:

  • For pages that are the children of a section instead of what you have I use .FirstSection.Title (see more about Section Variables here)

  • For taxonomies I resort to conditions like if in .Permalink "some-taxonomy" to render the parent title, as current Hugo variables do not make rendering a Taxonomy’s title in its children pages as straightforward as for Sections (Hugo Taxonomies are not hierarchical). The Doc mentions that entering metadata for taxonomies can be done by using an _index.md under the taxonomy PATH, but that is something I almost never use as I hate duplication and also it is a maintenance nightmare.

  • For author pages it depends on what you’re using to set the author page.

Anyway you should get the general idea. Unfortunately your head partial or block template is about to get pretty verbose but there is no way around it if you intend to render that parent Section or Taxonomy title for each page.

Perhaps others can share their own tips.

What you probably want is:

{{ $sectionTitle := .CurrentSection.Title }}

Yup. That’s better. Hard to find all the entry points to the Hugo “api”.

Although the get page trick would have helped with a problem I had about a month ago…