Weird pagination (can it be done with Hugo?)

I “so totally” would like a blog website where the following structure takes place:

homepage:

  • shows the latest entry full,
  • then 4 entries in short (either a list of titles that are linked or the .Summary with read more link)
  • then a numbered pager (first, before, 1, 2, 3, …, next, last)

list page

  • shows paged items (10 per page) without the 5 entries that were listed on the homepage.

Possible? Basically the listpage would have to take a range with the latest 5 items missing to realise this. Is there a way to give this info to the pagination?

You can start with something like in your partial list.html

{{ $paginator := .Paginate .Data.Pages }}
{{ range $index, $element := $paginator.Pages -}}
{{ if eq $index 1 }}
{{ .Content }}
{{ else }}
{{ .Render “summary” }}
{{end}}
{{- end }}
{{ partial “pagination” . }}

You can configure the pages that you send to the Paginator: Pagination | Hugo

Should be fairly straightforward:

<h1>First:</h1>
{{ range first 1 .Site.RegularPages }}
  <h2>{{.Title}}</h2>
  {{.Content}}
{{ end }}

<h1>Next 4:</h1>
{{ range first 4 (after 1 .Site.RegularPages) }}
  <h2>{{.Title}}</h2>
  {{.Summary}}
{{ end }}

<h1>Paginate:</h1>
<ul>
{{ range (.Paginate ( after 5 .Site.RegularPages ) 3 ).Pages }}
  <li>{{.Title}}</li>     <!-- force page size = 3 ^ for testing small number of pages -->
{{ end }}
</ul>

{{ template "_internal/pagination.html" . }}

Assuming the above where the first 5 on the homepage are just the latest 5 pages, regardless of content section, otherwise modify accordingly.

<!-- Get the latest 5 (should be equivalent to the latest 5 on homepage) -->
{{ $latesthome := ( first 5 .Site.RegularPages ) }}

<!-- Get the .Pages belonging to this section that are not in $latesthome -->
{{ $pages := .Pages | complement $latesthome }}

<h1>Paginate {{.Title}}:</h1>

<ul>
{{ range (.Paginate ( $pages ) 3 ).Pages }}
  <li>{{.Title}}</li>
{{ end }}
</ul>

{{ template "_internal/pagination.html" . }}

2 Likes

I am working on implementing this. The first part works fine, but the pagination is leading to the same content with /seite/2/ (/page/2/) in the url. I’ll try my own pagination script.

The pagination documentation is very helpful. Thanks for your help, I will post the end result once I have it working.

Do you mean the “First” and “Next 4” parts?

You could create the .Paginator before it is used (discussed in the Building the Navigation section of the pagination docs). Then show that part only on page 1. So:

{{ $paginator := (.Paginate ( after 5 .Site.RegularPages ) 3 ) }}

<!-- Check if on Page 1 -->
{{ if eq 1 $paginator.PageNumber }}
<h1>First:</h1>
...
<h1>Next 4:</h1>
...
{{ end }}

<h1>Paginate:</h1>
<ul>
{{ range $paginator.Pages }}
  <li>{{.Title}}</li>
{{ end }}
</ul>

...
1 Like

Also see:

1 Like