Paginate with an extra item on the first page?

I’m building a blog template that has a “featured” article, then X number of regular articles (lets say 6). Then pagination should have 6 more articles on each subsequent page. If I set paginate = 6 in the config, it all works fine except for the main page, which has the featured + 5 more. In a standard 3 columns per row grid, this doesn’t look good to have 5 entries.

Anyway I can override a single page to have Paginate+1?

Rough index.html as follows: https://gist.github.com/ShakataGaNai/24f09d054f490c00b91acd1da17d9d65

{{ define "main" }}
{{ $pages := where .Site.RegularPages "Type" "in" .Site.Params.mainSections }}
{{ $pag := .Paginate (where $pages "Params.hidden" "ne" true) }}
{{ $s := newScratch }}
{{ $s.Set "offset" "0" }}
<main class="homepage">
    {{ if not $pag.HasPrev }}
    {{ $s.Set "offset" "1" }}

    <!-- Hero -->
    {{ partial "hero.html" . }}

    <!-- Featured article -->
    {{ partial "featured.html" . }}
    {{ end }}
  
    <!-- Articles -->
    <div class="articles row row-cols-1 row-cols-lg-3">
        {{ $offset := $s.Get "offset" }}
        {{ range after $offset ($pag.Pages)}}
        <div class="col mb-3">
            {{ .Render "card" }}
        </div>
        {{ end }}
    </div>
    <div class="paginator row row-cols-1">
        {{ template "partials/paginator.html" . }}
    </div>
</main>
{{ end }}

You can override the global page size setting: https://gohugo.io/templates/pagination/#list-paginator-pages

Got it. Does work, but how do I increment the existing value? I can’t simple add 1 .Paginator.Pages

For my own personal usage, I could hard code everything, but trying to make it nice and user friendly.

Ok. In the end I realized it doesn’t matter. If I try to apply some logic like if eq .Paginator.PageNumber 1 then set number to X+1 else X… it doesn’t work. As soon as I invoke .Paginator.PageNumber the Paginator is created with the default values and I can’t override them.

So TLDR: I need to setup param in the config for front page count, separate from the regular paginator value.

… However this doesn’t work either because I have no way to detect that I’m on the homepage and NOT paginated. .isHome is always true… and I can’t use pagination or else I can’t override it. I’m lost, there has to be a solution here I’m not seeing.

So, you want your featured page to count towards the pages of the first paginator page?

Page 1
FXXXXX

Page 2
XXXXXX

Page 3
etc

where F is your featured page and X a regular page?

You could just add your featured page to the front of your pages to paginate. You would also need to make changes to how you render your featured page.

{{ $featuredpage := site.GetPage "yourfeaturedpage" }}
{{ $pages := slice ($featuredpage) }}
{{ $pages = $pages | append site.RegularPages }} <!-- add your other pages -->

{{ $paginator := .Paginate $pages }}
{{ template "_internal/pagination.html" . }} <!-- use your pagination template -->

<ol>
    {{ range $paginator.Pages }}
    <li>
        {{ if eq .Page $featuredpage  }}
        featured page here: {{.Page}}
        {{ else }}
        not a featured page: {{.Page}}
        {{ end }}
    </li>
    {{ end }}
</ol>
1 Like