[Solved] How would I make a list of links to sections?

It seems a simple question but I could not find reference to it in a lot of web searching. The most nearly on point was this “how to render all sections”.

My goal is to put a list of links to sections at the top of the page. Now, in this post I’m using the term “sections” for the first segment of the requested URL, after the domain name - please correct me if it’s the wrong term.

Imagine a site with these subdirectories in ‘content’:
blog
videos
reference

with some pages in each of those. I just want a list of links to the index (“list”) pages of those sections. Unlike the other poster above I do not want lists of pages in those sections - just the first part of the path as the url and the link text - for example looking like the list above, with links on those three strings (or whatever the section names are).

So I tried a modified version of the solution in the above post, as so:

<ul>{{ range $section := .Site.Sections }}
    <li><a href="/{{ $section }}">{{ $section }}</li>{{ end }}
</ul>

However this gets the following:

  • [WeightedPage(0,“about”)]
  • [WeightedPage(0,“test page title here”)]

Also I’m wondering whether these sections are related to the default taxonomy “categories”.

There’s an easier way to do this now. Try this and see if it gets you what you want:

{{ $sections := where (.Site.Pages) "Kind" "section" }}
  {{ range $sections  }}
    <li>
      <a href="{{ .Permalink }}">
        {{ .Title }}
      </a>
    </li>
  {{ end }}
3 Likes

Perfect! Thanks. I would have taken hours to find that syntax.

One detail for others who read this, by default it capitalizes the link text. I found the solution here (via this) - making the link line like this put it back to lowercase:

<a href="{{ .Permalink }}">{{ .Title | lower }}</a>

Note that this works only because my original value was all lowercase - if it is mixed case or all upper in the taxonomy and capitalization is unwanted, well see the links above.

A related tip; to get a specific section you can do:

{{ $section := .Site.GetPage "section" "blog" }}