diff --git a/layouts/_default/taxonomy.html b/layouts/_default/taxonomy.html
index bdf1f36b46..9bce1c753f 100644
--- a/layouts/_default/taxonomy.html
+++ b/layouts/_default/taxonomy.html
@@ -9,23 +9,8 @@
{{ $title }}
{{- .Content }}
{{- $lastCapital := "" }}
-{{- $pages := slice }}
-{{- range .Data.Terms }}
- {{- $len := 0 }}
- {{- range .Pages }}
- {{- $c:=""}}{{/* count pages of term */}}
- {{- if and .Title .RelPermalink (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableTagHiddenPages true) ) }}
- {{- $len = add $len 1 }}
- {{- end }}
- {{- end }}
- {{- if $len }}
- {{- $pages = $pages| append (dict "Title" (default (humanize .Page.Data.Term | strings.Title) .Page.Title) "Page" . "Len" $len )}}
- {{- end }}
- {{- if not .Page.Title }}
- {{- warnf "%q: You have not given a 'title' in the frontmatter of your term page, the humanized term will be used instead" .Page.File.Filename }}
- {{- end }}
-{{- end }}
-{{- range sort $pages ".Title" }}
+{{- $pages := partialCached "partials/pageHelper/taxonomyPages.html" . . }}
+{{- range $pages }}
{{- $capital := substr .Title 0 1 | upper }}
{{- if ne $lastCapital $capital }}
{{- if ne $lastCapital "" }}
diff --git a/layouts/_default/term.html b/layouts/_default/term.html
index e74548a8b5..05adc8f2e2 100644
--- a/layouts/_default/term.html
+++ b/layouts/_default/term.html
@@ -11,13 +11,8 @@
{{ $title }}
{{- .Content }}
{{- $lastCapital := "" }}
-{{- $pages := slice }}
-{{- range .Pages }}
- {{- if and .Title .RelPermalink (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableTagHiddenPages true) ) }}
- {{- $pages = $pages | append (dict "Title" .Title "Page" . )}}
- {{- end }}
-{{- end }}
-{{- range sort $pages ".Title" }}
+{{- $pages := partialCached "partials/pageHelper/termPages.html" . . }}
+{{- range $pages }}
{{- $capital := substr .Title 0 1 | upper }}
{{- if ne $lastCapital $capital }}
{{- if ne $lastCapital "" }}
diff --git a/layouts/partials/pageHelper/next.html b/layouts/partials/pageHelper/next.html
new file mode 100644
index 0000000000..a4d5024919
--- /dev/null
+++ b/layouts/partials/pageHelper/next.html
@@ -0,0 +1,13 @@
+{{- $collection := .collection }}
+{{- $item := .item }}
+{{- $found := false }}
+{{- $result := "" }}
+{{- range $collection }}
+ {{- if $found }}
+ {{- $result = .Page.Page }}
+ {{- break }}
+ {{- else if eq $item .Page.Page }}
+ {{- $found = true }}
+ {{- end }}
+{{- end }}
+{{- return $result }}
\ No newline at end of file
diff --git a/layouts/partials/pageHelper/prev.html b/layouts/partials/pageHelper/prev.html
new file mode 100644
index 0000000000..62a97514ed
--- /dev/null
+++ b/layouts/partials/pageHelper/prev.html
@@ -0,0 +1,12 @@
+{{- $collection := .collection }}
+{{- $item := .item }}
+{{- $found := "" }}
+{{- $result := "" }}
+{{- range $collection }}
+ {{- if eq $item .Page.Page }}
+ {{- $result = $found }}
+ {{- break }}
+ {{- end }}
+ {{- $found = .Page.Page }}
+{{- end }}
+{{- return $result }}
\ No newline at end of file
diff --git a/layouts/partials/pageHelper/taxonomyPages.html b/layouts/partials/pageHelper/taxonomyPages.html
new file mode 100644
index 0000000000..6174f1bc35
--- /dev/null
+++ b/layouts/partials/pageHelper/taxonomyPages.html
@@ -0,0 +1,15 @@
+{{- $pages := slice }}
+{{- range .Data.Terms }}
+ {{- $len := 0 }}
+ {{- range .Pages }}
+ {{- $c:=""}}{{/* count pages of term */}}
+ {{- if and .Title .RelPermalink (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableTagHiddenPages true) ) }}
+ {{- $len = add $len 1 }}
+ {{- end }}
+ {{- end }}
+ {{- if $len }}
+ {{- $pages = $pages| append (dict "Title" (default (humanize .Page.Data.Term | strings.Title) .Page.Title) "Page" . "Len" $len )}}
+ {{- end }}
+{{- end }}
+{{- $pages = sort $pages ".Title" }}
+{{- return $pages }}
\ No newline at end of file
diff --git a/layouts/partials/pageHelper/termPages.html b/layouts/partials/pageHelper/termPages.html
new file mode 100644
index 0000000000..aee80ef007
--- /dev/null
+++ b/layouts/partials/pageHelper/termPages.html
@@ -0,0 +1,8 @@
+{{- $pages := slice }}
+{{- range .Pages }}
+ {{- if and .Title .RelPermalink (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableTagHiddenPages true) ) }}
+ {{- $pages = $pages | append (dict "Title" .Title "Page" . )}}
+ {{- end }}
+{{- end }}
+{{- $pages = sort $pages ".Title" }}
+{{- return $pages }}
\ No newline at end of file
diff --git a/layouts/partials/toc-id.html b/layouts/partials/toc-id.html
index 8c54d26183..6d9e790d0e 100644
--- a/layouts/partials/toc-id.html
+++ b/layouts/partials/toc-id.html
@@ -2,27 +2,12 @@
{{- $lastCapital := "" }}
{{- $pages := slice }}
{{- if eq .Kind "taxonomy" }}
- {{- range .Data.Terms }}
- {{- $len := 0 }}
- {{- range .Pages }}
- {{- $c:=""}}{{/* count pages of term */}}
- {{- if and .Title .RelPermalink (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableTagHiddenPages true) ) }}
- {{- $len = add $len 1 }}
- {{- end }}
- {{- end }}
- {{- if $len }}
- {{- $pages = $pages| append (dict "Title" (default (humanize .Page.Data.Term | strings.Title) .Page.Title) "Page" . "Len" $len )}}
- {{- end }}
- {{- end }}
+ {{- $pages = partialCached "partials/pageHelper/taxonomyPages.html" . . }}
{{- else if eq .Kind "term" }}
- {{- range .Pages }}
- {{- if and .Title .RelPermalink (or (ne (.Scratch.Get "relearnIsHiddenStem") true) (ne .Site.Params.disableTagHiddenPages true) ) }}
- {{- $pages = $pages | append (dict "Title" .Title "Page" . )}}
- {{- end }}
- {{- end }}
+ {{- $pages = partialCached "partials/pageHelper/termPages.html" . . }}
{{- end }}
{{- $toc_pages := "" }}
- {{- range sort $pages ".Title" }}
+ {{- range $pages }}
{{- $capital := substr .Title 0 1 | upper }}
{{- if ne $lastCapital $capital }}
{{- $toc_pages = printf "%s %s\n" $toc_pages ($capital | plainify | anchorize) $capital }}
diff --git a/layouts/partials/topbar/button/next.html b/layouts/partials/topbar/button/next.html
index 44eb0c5c11..b36fabd423 100644
--- a/layouts/partials/topbar/button/next.html
+++ b/layouts/partials/topbar/button/next.html
@@ -11,7 +11,16 @@
{{- $endarrow = "🡐" }}
{{- end }}
{{- $next := "" }}
- {{- if or (ne $outputFormat "html") (not (.Scratch.Get "relearnNextPage")) }}
+ {{- if eq .Page.Kind "taxonomy" }}
+ {{- /* go to first term page */}}
+ {{- $pages := partialCached "partials/pageHelper/taxonomyPages.html" . . }}
+ {{- $next = (index $pages 0).Page.Page }}
+ {{- else if eq .Page.Kind "term" }}
+ {{- /* go to next term page */}}
+ {{- $taxonomy_page := .Site.GetPage .Data.Plural }}
+ {{- $pages := partialCached "partials/pageHelper/taxonomyPages.html" $taxonomy_page $taxonomy_page }}
+ {{- $next = partial "partials/pageHelper/next.html" (dict "collection" $pages "item" .) }}
+ {{- else if or (ne $outputFormat "html") (not (.Scratch.Get "relearnNextPage")) }}
{{- else }}
{{- $next = .Scratch.Get "relearnNextPage" }}
{{- end }}
diff --git a/layouts/partials/topbar/button/prev.html b/layouts/partials/topbar/button/prev.html
index bbabf82765..63b0c18b90 100644
--- a/layouts/partials/topbar/button/prev.html
+++ b/layouts/partials/topbar/button/prev.html
@@ -12,7 +12,17 @@
{{- end }}
{{- $prev := "" }}
{{- if and (eq .RelPermalink .Site.Home.RelPermalink) (eq $outputFormat "html") }}
- {{- else if or (ne $outputFormat "html") (not (.Scratch.Get "relearnPrevPage")) (eq .Page.Kind "taxonomy") (eq .Page.Kind "term") }}
+ {{- else if eq .Page.Kind "taxonomy" }}
+ {{- $prev = .Site.Home }}
+ {{- else if eq .Page.Kind "term" }}
+ {{- /* go to previous term page or taxonomy page if it is the first term */}}
+ {{- $taxonomy_page := .Site.GetPage .Data.Plural }}
+ {{- $pages := partialCached "partials/pageHelper/taxonomyPages.html" $taxonomy_page $taxonomy_page }}
+ {{- $prev = partial "partials/pageHelper/prev.html" (dict "collection" $pages "item" .) }}
+ {{- if not $prev }}
+ {{- $prev = $taxonomy_page }}
+ {{- end }}
+ {{- else if or (ne $outputFormat "html") (not (.Scratch.Get "relearnPrevPage")) }}
{{- $prev = .Site.Home }}
{{- else }}
{{- $prev = .Scratch.Get "relearnPrevPage" }}