Hello,
for the last two days I am trying to display on page paginated regular page recursive
but unfortunately without a success.
My pages:
- /posts/
- /posts/category-a/
- /posts/category-a/page1.html
- /posts/category-b/
- /posts/category-b/page2.html
- /posts/category-b/page3.html
I would like to display paginated list of all posts in /posts/
. Method .RegularPagesRecursive
seems to be what I am looking for. And it works when I save the result to the variable.
{{ $pages := .RegularPagesRecursive }}
{{ printf "%v" (len .RegularPagesRecursive) }}
gives the appropriate results:
- 3 for
/posts/
- 1 for
/posts/category-a/
- 2 for
/posts/category-b/
What DOES NOT work is pagination. Every time I paginate set of pages, I receive the same result.
{{ $pages := .RegularPagesRecursive }}
{{ $paginator := .Paginate $pages }}
{{ printf "%v" $paginator.TotalNumberOfElements }}
gives:
- 0 for
/posts/
- 1 for
/posts/category-a/
- 2 for
/posts/category-b/
If I change the rule which should give always the same number (3), again I will return the same results.
{{ $pages := where site.RegularPages "Type" "in" site.Params.postSection }}
{{ printf "%v" (len $pages) }}
{{ $paginator := .Paginate $pages }}
{{ printf "%v" $paginator.TotalNumberOfElements }}
gives:
- 3 0 for
/posts/
- 3 1 for
/posts/category-a/
- 3 2 for
/posts/category-b/
What do I do wrong? I edit themes/sports/layouts/_default/list.html
file.
I am aware the comment in the documentation:
If you call .Paginator or .Paginate multiple times on the same page, you should ensure all the calls are identical. Once either .Paginator or .Paginate is called while generating a page, its result is cached, and any subsequent similar call will reuse the cached result. This means that any such calls which do not match the first one will not behave as written.
but I don’t use pagination anywhere else.
I would be very helpful for you help.
Type tree content/posts
and paste the results here.
tree content/posts
:
content/posts
├── _index.md
├── glowna-kategoria
│ ├── _index.md
│ └── pros-and-cons-of-iphone-12.md
├── kotki
│ ├── _index.md
│ ├── with-category.md
│ └── your-new-story-url.md
└── pieski
└── _index.md
3 directories, 7 files
There are only 3 regular pages in your tree.
If you want to create a paginated list of all regular pages in the content/posts directory:
{{ $p := where site.RegularPages "Type" "posts" }}
{{ range (.Paginate $p).Pages }}
<h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}
{{ template "_internal/pagination.html" . }}
Is {{ template "_internal/pagination.html" . }}
required? What is it file?
Having just:
{{ $p := where site.RegularPages "Type" "posts" }}
{{ range (.Paginate $p).Pages }}
<h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
{{ end }}
gives result:
-
/posts
- blank page (which is INCORRECT)
-
/posts/glowna-kategoria
- displays 1 title (which is fine)
-
/posts/kotki
- displays 2 titles (which is fine)
-
/posts/pieski
- blank page (which is fine)
The thing is that if I change:
{{ $p := where site.RegularPages "Type" "posts" }}
to something which doesn’t exist e.g.:
{{ $p := where site.RegularPages "Type" "offers" }}
I will receive exactly the same results.
https://gohugo.io/templates/pagination/#build-the-navigation
Without seeing your entire project, I have no idea what you are doing wrong. So here’s a test site you can try. All “posts” are listed on the home page, paginated, 5 pages per pager.
git clone --single-branch -b hugo-forum-topic-41349 https://github.com/jmooring/hugo-testing hugo-forum-topic-41349
cd hugo-forum-topic-41349
hugo server
Thank you for the example. It works there perfectly.
I finally found the problem. I had been looking for other .Paginate
usage, but I didn’t notice that in head.html
I have usage of .Paginator
which causes the problem. I am surprised I didn’t find it before.
part of head.html
{{ if .Params.meta.canonical }}
{{ if .Paginator }}
{{ if ne .Paginator.PageNumber 1 }}
<link rel="canonical" href="{{ .Params.meta.canonical | strings.TrimSuffix "/" }}/page/{{ .Paginator.PageNumber }}/"/>
<meta property="og:url" content="{{ .Params.meta.canonical | strings.TrimSuffix "/" }}/page/{{ .Paginator.PageNumber }}/"/>
{{ else }}
<link rel="canonical" href="{{ .Params.meta.canonical | strings.TrimSuffix "/" }}/"/>
<meta property="og:url" content="{{ .Params.meta.canonical | strings.TrimSuffix "/" }}/"/>
{{ end }}
{{ else }}
<link rel="canonical" href="{{ .Params.meta.canonical | strings.TrimSuffix "/" }}/"/>
<meta property="og:url" content="{{ .Params.meta.canonical | strings.TrimSuffix "/" }}/"/>
{{ end }}
{{ end }}
my baseof.html
<!DOCTYPE html>
<html lang='{{ .Site.LanguageCode }}'>
{{ partial "head.html" . }}
<body class="flex m-auto min-h-screen xl:max-w-screen-xxl">
<main class="w-full">
{{- block "main" . }}{{- end }}
</main>
{{ if $.Site.Data.global.scripts }}
{{ $.Site.Data.global.scripts.body | safeHTML }}
{{ end }}
</body>
</html>
How should I adjust my code to could in <head></head>
set appropriate canonical urls but later in list.html
use custom pagination?