Search code examples
hugostatic-site

Sorting Hugo Homepage Post Summaries by Title with Hyde-Hyde theme


I created a static site with Hugo, using the Hyde-Hyde theme, but I cannot get the post-summaries on the homepage (or the posts page) to be sorted by date. I know using a different theme would fix the post-sorting, but this question is specifically about getting it to work with the Hyde-Hyde theme. Reproducible code example below:

Here's the code to get the a new Hugo site using the Hyde-Hyde theme (and its exampleSite contents):

~/$ hugo new site mySite
~/$ cd mySite
~/mySite$ git clone https://github.com/htr3n/hyde-hyde.git themes/hyde-hyde
~/mySite$ rm -rf themes/hyde-hyde/.git themes/hyde-hyde/.gitmodules
~/mySite$ mv themes/hyde-hyde/exampleSite/* .

we can now build the site and serve it locally with:

~/mySite$ hugo
~/mySite$ hugo serve

...which tells us that our site is available at http://localhost:1313/

If you click the localhost link you will see a site similar to (though not identical to) the "Demo" page linked from the Hyde-Hyde theme homepage. However, posts on the homepage (and 'posts' page) are not ordered by date; you'll notice the first three posts are dated September 2014, then March 2014, then April 2014.

Per the github issue I filed, so far I've tried changing

    {{ with .Data.Pages }}

to

    {{ with .Data.Pages.ByDate }}

in themes/hyde-hyde/layouts/partials/page-list/content.html and

{{ range . }}

to a few different things in themes/hyde-hyde/layouts/partials/posts-list.html,

but I have not been able to get the posts to show up ordered by date on the homepage and 'posts' page.


Solution

  • Regarding the post order on the homepage, Maiki solved this on the hugo discourse page. Putting his answer below in block quotes:

    """

    That is the home page, which in that theme is rendered by layouts/index.html:

    {{ $paginator := .Paginate (where .Site.RegularPages "Type" "in" site.Params.mainSections) }}
    {{ range $paginator.Pages }}
    

    That’s setting your paginator, and then range through it. You probably want:

    {{ range $paginator.Pages.ByDate.Reverse }}
    

    Note it is using the default sort order for content, and the Sep. post is weighted while the other posts are not. That can affect the listings, as well.

    """

    ...For the other part of my question, getting the posts sorted right on the 'posts' page, the solution was to change

    {{range .}}
    

    to:

    {{ range .ByDate.Reverse }}
    

    in themes/hyde-hyde/layouts/partials/posts-list.html