Section custom Title cascaded to nested sections and pages

I have a use case for a section title in a multilingual site that I need help. Let’s assume the section is called news with an seoTitle of News. I want to assign the <title></title> value in head.html as follows.

  1. The section itself gets the value of seoTitle in _index.md.
  2. The nested sections and pages get the value of their title and also append the seoTitle from news _index.md at the end.

How to achieve this?

You’ll need to access the parent page, something like .Page.Parent.Params.seoTitle.

Which is the correct way to target the section (section’s _index.md) only? Coz in my code I have to assign a weight to it as below.

      {{- $title := .Title }}
{{- if and (.IsSection) (ne .Layout "contact") }}
    {{- if (eq .Section "news") }}
      {{- if .Page.CurrentSection.Params.seoTitle }}
      {{- $title = .Params.seoTitle }}
      {{- else }}
      {{- $title = (printf "%s | %s" $title .Page.Parent.Params.seoTitle  ) }}
      {{- end }}
    {{- else }}
    {{- $title = (printf "%s | %s" $title site.Title ) }}
    {{- end }}
{{- end }}

Compare .Page.Kind to “section” or test for Page.IsSection. Using weight is fragile.

You can also use .Page.CurrentSection.Params.foo. This is slightly different than .Parent because you can use it with the section page too.

I made an edit to the code to show the full code because the <title> is empty for nested sections

If you need to get to the top use .FirstSection.

Still empty. Edited the code.

I should have been clearer. You can use .FirstSection instead of .Parent if you need to get the top level section page, skipping the nested sections in between.

What I mean is that, any usage of FirstSection makes the <title> attribute for nested sections to disappear in <head>.

This doesn’t make any sense.

This also returns empty

No, the .IsSection method will always return true or false.

I forgot this also works. But I wish there was a straightforward way to only target the section only.

{{- if .Page.FirstSection.Params.seoTitle }}

I think you take a break and revisit this later.

Returns some broken code %!s(&lt;nil&gt;). I will stick with FirstSection,

Actually I always use workarounds for these issues. Working with nested sections is a headache.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.