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.