Access first page from page collection

I’m trying to make a sidebar partial refering to subsections in my “Test” section. I want to use the .Title of the subsection, but the reference should go to the first content page inside this subsection. Right now I’ve got the following:

{{ $currentPage := . }}
{{ range .Site.Sections }}
{{ if eq .Title "Test" }}
{{ range .Pages }}

  {{ if .IsSection }}
    <!-- the following line should refere to the fist subpage instead -->
    <a href="{{ .RelPermalink }}">
      <h6>{{ .Title }}</h6>
    </a>
  {{ if .InSection $currentPage }}
  {{ range .Pages }}
  ...
  {{ end }}
  {{ end }}
  {{ end }}
{{ end }}
{{ end }}
{{ end }}

.Title is working as inteded and giving the headline of the subsection. But the .RelPermalink should be replaced with the RelPermalink of the first content page in this section.

Is there a way to get the permalink of the first element in the collection, like .Pages[0].RelPermalink ?

I’ve tried to use the .Prev method, but I don’t know what to pass as argument. I’ve tried

<a href="{{ with .Pages.Prev . }}{{ .RelPermalink }}{{ end }}" class="d-block">

but it seemed like .Pages.Prev is nil if I hand a collection as argument.

I’m quite new to Hugo, so thanks for helping! :slight_smile:

Use the index func, e.g.

{{ (index .Pages 0).RelPermalink }}
3 Likes

thanks! Hugo is awsome :heart_eyes:

1 Like

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