# Sorting order for pages

**URL:** <https://discourse.gohugo.io/t/sorting-order-for-pages/55570>\
**Category:** support\
**Created:** [August 5, 2025, 5:26pm UTC](https://discourse.gohugo.io/t/sorting-order-for-pages/55570 "2025-08-05T17:26:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![swehrend](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/swehrend/32/22923_2.png) [@swehrend](https://discourse.gohugo.io/u/swehrend)\
**Post date:** [August 5, 2025, 5:26pm UTC](https://discourse.gohugo.io/t/sorting-order-for-pages/55570/1 "2025-08-05T17:26:45Z")

</div>

Hello hugoers,

I’ve wanted to implement a couple of buttons (previous and next) at the bottom of the page (for pages not for posts). I am using the hugo-book and hugo version 0.148.1 .

I’ve added the snippet below into the footer but I am experiencing some trouble / inconsistences with the order of the pages. I tried it with weight but that did not work as it should. I am bit confused.  
What is the best way to do this. I wanted also weight-per-category so that i can handle different topics on one site..

```auto
<div class="flex justify-between w-full mt-4 text-sm text-gray-600">
  {{ if gt $index 0 }}
    {{ $prev := index $sorted (sub $index 1) }}
    <a href="{{ $prev.RelPermalink }}">← Vorheriger Beitrag: {{ $prev.Title }}</a>
  {{ end }}

  {{ if lt (add $index 1) (len $sorted) }}
    {{ $next := index $sorted (add $index 1) }}
    <a href="{{ $next.RelPermalink }}" class="ml-auto">Nächster Beitrag: {{ $next.Title }} →</a>
  {{ end }}
</div>

```

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [August 5, 2025, 6:42pm UTC](https://discourse.gohugo.io/t/sorting-order-for-pages/55570/2 "2025-08-05T18:42:43Z")

</div>

Have you considered using:

- [https://gohugo.io/methods/pages/prev](https://gohugo.io/methods/pages/prev)
- [https://gohugo.io/methods/pages/next](https://gohugo.io/methods/pages/next)

Let’s handle your “weight-per-category” issue after resolving the prev/next issue.

---

<div class="post-metadata">

**Author:** ![swehrend](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/swehrend/32/22923_2.png) [@swehrend](https://discourse.gohugo.io/u/swehrend)\
**Post date:** [August 5, 2025, 6:52pm UTC](https://discourse.gohugo.io/t/sorting-order-for-pages/55570/3 "2025-08-05T18:52:16Z")

</div>

I will look into this, thanks…

---

<div class="post-metadata">

**Author:** ![wehrend](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/wehrend/32/22842_2.png) [@wehrend](https://discourse.gohugo.io/u/wehrend)\
**Post date:** [August 10, 2025, 3:45pm UTC](https://discourse.gohugo.io/t/sorting-order-for-pages/55570/4 "2025-08-10T15:45:32Z")

</div>

```auto
I ended up with some custom logic in the footer in the template so that I can order by category and name- that satisfied my needs:

```

```auto

{{/* Kontext */}}
{{ $current := . }}
{{ $category := .Params.category }}

{{/* Nur reguläre Seiten in Section "pages", gleiche Sprache */}}
{{ $all := where (where .Site.RegularPages "Kind" "page") "Section" "pages" }}
{{ $all = where $all "Lang" .Lang }}

{{/* Nur gleiche Kategorie wie aktuelle Seite */}}
{{ $pages := where $all "Params.category" $category }}

{{/* In "000_"-Ordner und normale Seiten aufteilen */}}
{{ $special := slice }}
{{ $normal := slice }}

{{ range $p := $pages }}
  {{ if $p.File }}
    {{ $segments := split (trim $p.File.Dir "/") "/" }}
    {{ $dirname := index $segments (sub (len $segments) 1) }}
    {{ if hasPrefix $dirname "000_" }}
      {{ $special = $special | append $p }}
    {{ else }}
      {{ $normal = $normal | append $p }}
    {{ end }}
  {{ else }}
    {{ $normal = $normal | append $p }}
  {{ end }}
{{ end }}

{{/* Innerhalb der Gruppen alphabetisch nach Title sortieren */}}
{{ $specialSorted := sort $special "Title" }}
{{ $normalSorted := sort $normal "Title" }}

{{/* Finales Slice ohne variadics zusammenbauen */}}
{{ $final := $specialSorted }}
{{ range $normalSorted }}
  {{ $final = $final | append . }}
{{ end }}

{{/* Index der aktuellen Seite finden */}}
{{ $index := -1 }}
{{ range $i, $p := $final }}
  {{ if eq $p.Permalink $current.Permalink }}{{ $index = $i }}{{ end }}
{{ end }}

<div class="flex justify-between w-full mt-4 text-sm text-gray-600">
  {{ if gt $index 0 }}
    {{ $prev := index $final (sub $index 1) }}
    <a href="{{ $prev.RelPermalink }}">← {{ $prev.Title }}</a>
  {{ end }}
  {{ if lt (add $index 1) (len $final) }}
    {{ $next := index $final (add $index 1) }}
    <a href="{{ $next.RelPermalink }}" class="ml-auto">{{ $next.Title }} →</a>
  {{ end }}
</div>

```
