Links to second level section pages from child pages

So not used Hugo in a while but I’m struggling with what I’m sure is a very basic problem. So apologies for that but have searched around and experimented quite a bit and not found a solution.

So my site has a simple geographical based hierarchy. The top level is regions, so the top level content folders are N Wales, S Wales, Yorkshire etc. Each region has different areas and each area has specific places. The minor complication here is the places are sometimes single regular pages and sometimes mini sections with their own _index.md page and other regular pages.

I want a menu to link back up the heirarchy from every place’s page. To link back to the specific region of a place was easy using .FirstSection:

{{ $thispage := . }}
 {{ with .FirstSection }}
       {{ if ne $thispage . }}
                <a href="{{ .Permalink }}">{{ .Title }}</a>
       {{ end }}
 {{ end }}

What I want now would be the .SecondSection equivalent but of course it doesn’t exist.

I can almost get what I want using range:

{{ range .FirstSection.Pages }}
    <li><a href="{{.Permalink }}">{{ .LinkTitle }}</a></li>
{{ end }}

This produces a list of all the area section pages in the current region. I only want the one link to the area that contains it’s place’s pages. I’ve tried a few things with .Parent and also filtering the above range list with .IsAncestor but with no success and felt that was probably overcomplicating things.

Any help and suggestions would be much appreciated. Thanks

If your content structure looks like this:

content/
├── region/
│   ├── area/
│   │   ├── place-1/
│   │   │   ├── foo.md
│   │   │   └── _index.md
│   │   ├── _index.md
│   │   └── place-2.md
│   └── _index.md
└── _index.md

You can use:

{{ with .Parent }}
  <a href="{{ .RelPermalink }}">{{ .Title }}</a>
{{ end }}

Hi and thanks for the quick reply. That is the file structure and the .Parent code works from the section pages. However it doesn’t work from place-1/foo.md as the parent is then place-1/_index.md.

So I could split this with an if statement, something like:

{{ if and (eq "Kind" "Section") (.Parent) }}
  <a href="{{ .Parent.Permalink }}">{{ .Parent.Title }}</a>
{{ else }} 
???
{{ end }}

but I’m not sure what to use for the second part. I tried .Parent.Parent.Permalink but looks like that is not valid.

So the parent of foo should be area, even though foo is a child of place-1? Please confirm.

No, from foo.md its the _index.md of place-1.

Try this, and tell me what you want to be different:

git clone --single-branch -b hugo-forum-topic-34015 https://github.com/jmooring/hugo-testing hugo-forum-topic-34015
cd hugo-forum-topic-34015
hugo server

Thanks for that. So from content/region/area/place-1/foo.md going up one level goes to Place-1. I want it to go up to Area (two levels).

In reality place-1 and foo both pages about the same place so they both want to link back to their area.

That what I asked you earlier…

Give me a few minutes…

If it helps at all here is an example.
http://www.sportsclimbs.co.uk/mainpages/peak/matlock/turkey-dip-rocks.htm

This is not a Hugo site, it’s fully hand coded and a nightmare to maintain with about 500 pages. The project is a rebuild with Hugo. The right sidebar is the menu I want to replicate. Here, Turkey Dip Rocks is the place listed in the area page, Turkey Dip topo is a second page, Matlock the area and Peak District the region.

I’m off to bed. It’s late here in the UK. Thanks again for your help.

{{ $level := 0 }} {{/* Root of content directory. */}}
{{ with .Parent }}
  {{ $level = 1 }}
  {{ with .Parent }}
    {{ $level = 2 }}
    {{ with .Parent }}
      {{ $level = 3 }}
      {{ with .Parent }}
        {{ $level = 4 }}
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}

{{ with .Parent }}
  {{ if eq $level 4 }}
    <a href="{{ .Parent.RelPermalink }}">{{ .Parent.Title }}</a>
  {{ else }}
    <a href="{{ .RelPermalink }}">{{ .Title }}</a>
  {{ end }}
{{ end }}

Fantastic. Thanks so much. I wouldn’t have come up with that on my own. As I didn’t want this link to show up at all in pages of the area level or above I made a minor modification (line 4) and all’s well:

{{ with .Parent }}
    {{ if eq $level 4 }}
    <a href="{{ .Parent.RelPermalink }}">{{ .Parent.LinkTitle }}</a>
    {{ else if eq $level 3 }}
    <a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
    {{ end }}
{{ end }}

Quite a bit of code for one link though. Would be good if a future version of Hugo could have .SecondSection and even .ThirdSection perhaps?

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