Hi everyone! Came to the project with couple of content md files that has some props in frontmatter section, like this:
title: ...
name:
icon: ...
category: study
cardTitle: ...
what’s important there are no date or PublishDate fields.
But latter in the html there is such code that loopes through abovementioned md files basing on ByPublishDate and renders these cards on the page.
{{ $pages := .Pages.ByPublishDate.Reverse }}
<div class="cardClass">
{{ range where $pages "Params.category" "study" }}
So when i add a new md file it take place not in order I would expect (it takes a place in the middle, not in the end or at the beggining of cards positioning). While experimenting I found out, that the field being used for sort - is title. I studied Lists of Content in Hugo | Hugo page, but it doesn’t cover my case.
1 ) What is the default sort field from frontmatter, if value mentioned in range functionality is not present?
2) Could somebody please explain such behaviour?
3) The best option to fix such an unpredicted behaviour would be to add date attribute to frontmatter part of md files, right?
Depending on the number of articles you have, if you don’t want to have dates at all, then weight is the best option. Hugo prioritizes weight from negative to positive, so you can choose the sequence you want for your weights, bearing in mind the future and how often you post. I have a section in my site without dates and I opted for weight in the files themselves from 1000 going up for existing files and any new contents get the weight in reverse order, i.e 999, 998 etc. Then you can sort by weight in your templates. I am not sure how you can tell the sequence of the posts from the oldest to the current one, but if you can, then setting weight: xx in the front matter is the way to go.
Though as Bep says, using dates is better and that does not mean you have to display them in your content.