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..
<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>
Have you considered using:
Let’s handle your “weight-per-category” issue after resolving the prev/next issue.
I will look into this, thanks…
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:
{{/* 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>