List pages in nested section

I have built a site where I have organised the content into nested sections. I have the following nested sections:

content

  • interviews
  • blog
  • education
    • get-started
    • get-serious

I am trying list the pages (and get the first 3) in the get-started nested section. I want to do this on the home page.

I am successfully able to list the pages in a section but I’ve not been successful at doing it for nested sections.

Would someone be able to assist me to create the code to get this information please?

You should be able to use

{{ $getstarted := site.GetPage "/education/get-started" }}

{{ with $getstarted }}
  {{ range first 3 .Pages }}
    {{.Title}}<br>
  {{ end }}
{{ end }}

Thank you.

What I found confusing is that to get the pages from a section I run the following code.

			{{ $articles := where .Site.RegularPages.ByPublishDate.Reverse "Section" "interview" }}
	<div class="articles">
		{{ range $index, $element := first 3 $articles  }}
			{{ $permalink := .Permalink }}
			{{ with .Resources.GetMatch "portrait.*" }}
		   		{{ if .RelPermalink }}
					<a href="{{ $permalink }}"> <img src="{{ .RelPermalink }}"> </a>
		   		{{ end}}
			{{end}}
	{{ end }}

Why is there such a difference in the code between getting the pages for a section and a subsection? The difference could be that I am misunderstanding the best way to structure my code or not understanding some of the new developments of Hugo. If this is the case, I would like to understand those new developments.

1 Like

From the docs:

.Section

The section this content belongs to. Note: For nested sections, this is the first path element in the directory, for example, /blog/funny/mypost/ => blog .

So /posts/deeply/nested/lorem.md and /posts/ipsum.md will both have posts as their Section, and both included in:

{{ range where .Site.RegularPages "Section" "posts" }}