Order paginated posts based on frontmatter

Just moving from Jekyll. However, my paginated posts in index and category pages are showing alphabetically. I would like to sort them by lastmod which is defined in each posts frontmatter like lastmod: Nov 02, 2021, but also exclude one category under folder /content/posts/views. Right now I am using this code

{{ $paginator := .Paginate (where .Pages "Type" "posts") }} 
	 {{ range $paginator.Pages }}
      // Content Goes Here
     {{ end }}

How do I achieve this? I would appreciate a working code also.

1 Like

The documentation has a dedicated page about grouping content including an example By LastMod:

I have lastmod in frontmatter as Nov 01, 2021 for example but it returns blank.

I cannot know what’s going on without seeing the project or if that is not possible please share a minimal dummy project that reproduces the issue you describe.

This cannot be parsed to a time.Time value. See:
https://github.com/gohugoio/hugo/issues/8285#issuecomment-813682508

You could use 01 Nov 2021 or 2021-11-01 instead.

Hugo will catch this error if your front matter is TOML and you use datetime values instead of strings.

@jmooring can I request this to be added on GitHub?

Sure, but I suspect the request will be rejected.

I will stick with Jekyll for now then for my main project. But will move the smaller ones

If your editor/IDE supports regex search and replace, this is a trivial exercise. For example, in Microsoft Visual Studio Code:

Search:

^lastmod:\s*([A-Z]{1}[a-z]{2})\s+(\d{2}),\s+(\d{4})

Replace:

lastmod: $2 $1 $3

Alright. Done! Thanks. I opened a new support topic about the pagination overriding the number of posts when a certain category is excluded. This post can be closed now.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.