HUGO

Ordering both .RegularPages and .Sections

Hello dear Hugo Community,

This is my first topic. With a webmster partner, I built my website inspired from the theme ANANKE (Ananke | Articles). From now I have to fly with my own wings, but I need your help.

I have a navigation interface with images, which I would like to order its as I defined the position parameter in the front matter of each page:

  • Valeurs : 1
  • Marque : 2
  • Récompenses : 3
  • Engagements : 4
  • Biographie : 5
  • Methodes : 6

But as you can see, it is not the expected order.

This is because there are two .RegularPages (Récompenses and Engagements) and four .Sections of _index.md pages as illustrated in the tree structure.
Arboresences_Content

Below is the current code.

<section class="flex">
  {{ with .RegularPages }}
    {{ range sort . ".Params.position" }}
      <div class="w-20">
        {{- partial "summary-with-image.html" . -}}
      </div>
    {{ end }}
  {{ end }}

  {{ with .Sections }}
      {{ range sort . ".Params.position" }}
        <div class="w-20">
          {{- partial "summary-with-image.html" . -}}
        </div>
      {{ end }}
  {{ end }}
</section>

Idealy, I would need a shorter code as below, gathering both typology of pages, but I don’t have the right way.

<section class="flex">
  {{ with .RegularPages }} OR {{ with .Sections }}
    {{ range sort . ".Params.position" }}
      <div class="w-20">
        {{- partial "summary-with-image.html" . -}}
      </div>
    {{ end }}
  {{ end }}
</section>

Thanks a lot for your help.

Use menu and weight. In the frontmatter of the 4 section’s _index.html and in engagements.md and recompenses.md add something along the following:

---
menu:
  imagenav:
    weight: 10
---

Use weight to sort the items (I tend to use multiples of 10, so it’s easier to add a new item in between without having to touch all items.

Then range through the navigation like this:

{{ range site.Menus.imagenav }}
do something
{{ end }}

Have a read through the menu doc for details…

1 Like