Order posts by date and Param, with pagination

Hi,

I’m struggling to achieve the following:

  • Display all posts
  • By date (more recent to more old)
  • But displaying 3 “featured” first

For instance:

PAGE 1

  • Featured 1 (most recent featured)
  • Featured 2 (second most recent featured)
  • Featured 3 (third most recent featured)
  • Regular 1 (most recent regular)
  • Regular 2 (second most recent regular)

PAGE 2

  • Reguler 3
  • Regular 4
  • Regular 5
  • Regular 6
  • Regular 7

PAGE 3

  • Etc

I though to be successful with the following code:

// layouts/index.html

{{ define "main" }}
  <div>
    {{ with .Site.GetPage "posts" }}

      <!-- Only display featured posts on first page -->
      {{ if eq .Paginator.PageNumber 1 }}
        <div class="featured-list">
          {{ range where .Pages ".Params.featured" true }}
          {{ partial "post/summary.html" . }}
          {{ end }}
        </div>
      {{ end }}

      <!-- Regular posts -->
      {{ range where .Paginator.Pages ".Params.featured" "ne" true }}
        {{ partial "post/summary.html" . }}
      {{ end }}

    {{ end }}

    <!-- Pagination navigation -->
    {{ template "_internal/pagination.html" . }}
  </div>
{{ end }}

But the pagination seems broken…

Any idea?

A simple approach…

In the front matter of the posts that you wish to feature, set weight to a negative number (the default is zero). For example, you could consider every post with a weight of -999 to be “featured.”

Then, as you range through the pages, conditionally set a class on a containing <div>:

{{- if eq .Weight -999 -}}
<div class="featured">
{{- else -}}
<div class="not-featured">
{{- end -}}
{{ .Summary }}
</div>

See:
https://gohugo.io/templates/lists/#order-content
https://gohugo.io/variables/page/ (.Weight)

Hi @jmooring

Thanks for your answer. My issue is not to display featured first, but to make it work with a pagination.

use Paginator like this

{{ $paginator := .Paginate (where .Pages “Type” “posts”) 5 }}

you can create a list of pages with the wanted order and feed it to the paginator

Hi @ju52

Thanks for your answer. The documentation you quoted would allow me to paginate through my “featured” posts, or my “regular” posts, but what about a pagination containing all of them, starting by the featured ones?

you can make different selection with where and use union to get both, like the samples in

@ju52 Thanks again for your time.

It took me some hard times, but I think I managed to achieve my goal… Finding a source of a lot of my pagination issues in the process.

Using {{ with .Site.GetPage "posts" }} to fetch the posts was a bad idea. It has made the page inherit for the whole /posts/ behavior, such as display /posts/pages/2 for pagination.
I stored the context in a variable instead: $posts := .Site.GetPage "posts".

Here is how I managed to display all my posts, from featured to regular, while being sorted chronologically (from most recent to the most old), with a working pagination.

{{ define "main" }}
  <div>
    <!-- 1. Get all posts -->
    {{ $posts := .Site.GetPage "posts" }}

    <!-- 2. Sort them from featured to regular, chronologically -->
    {{ $featuredPosts := where $posts.Pages ".Params.featured" true }}
    {{ $regularPosts := where $posts.Pages ".Params.featured" false }}
    {{ $sortedPosts := union $featuredPosts $regularPosts }}

    <!-- 3. Feed the paginator with these sorted posts -->
    {{ $paginator := .Paginate $sortedPosts }}

    <!-- 4. Loop through the posts -->
    {{ range $paginator.Pages }}
      {{ partial "post/summary.html" . }}
    {{ end }}

    <!-- 5. Display the built-in pagination navigation -->
    {{ template "_internal/pagination.html" . }}

  </div>
{{ end }}

Does that sounds the right solution? Feel free to make any feedback, I’m struggling to master the “GoHugo way of do things”.