[Solved] Content summaries not sorted by weight

I had an old site made with Hugo in 2015 with different articles. The home page display a list of articles title and summary. They were properly sorted.

Now using the latest Hugo version v0.28, they are not sorted by weight anymore.

The directory is organized as follow:


Note: the css is partial because it is inlined in files.

The list is displayed in the index file with

<div id="idx-content">
{{range .Data.Pages}}
    {{.Render "summary"}}

Unfortunately, the ordering of the file summaries is not sorted by weight. Adding .ByWeight after .Data.Pages doesn’t help.

Is there a way I could fix that ?

Well I don’t have enough to troubleshoot this. Link to your site’s repo, and I will take a look.

If you are saying it isn’t working with a new version of Hugo, then you need to check all the templates generating that page, as well as the front matter in question, to ensure it is consistent and correct.

Thank you for offering to help. The repo is not public, sorry.

The matter of each file trivial:

date = "2017-09-27T19:00:00+01:00"
draft = false
title = "Distributed Information Reference"
shorttitle = "DIR"
author = "..."
weight = 10

It’s the same for each file, with a different title and weight of course. I removed the author name.

This is the summary file :

{{ .Title }}

{{ .Params.author}} - {{ .Date.Format “Mon, Jan 2, 2006” }}

{{ .Summary }}

{{ if .Truncated }}
Read More…
{{ end }}

The index file is this:

{{ partial "header.html" . }}
<div class="idx-logo">
    <img src="/img/Logo-v1.1-medium.jpg">
    <div class="text">
        <h1>{{ .Title }}</h1>
        <p>Home site of the Distributed Information System specification</p>
        <p>Work in progress ! For more information, contact... </p>
<div id="idx-content">
{{range .Data.Pages.ByWeight }}{{.Render "summary"}}{{end}}
{{ partial "footer.html" . }}

I simply removed the contact info.
The footer is simply


The header.html is the following:

<!DOCTYPE html>
    <meta charset="utf-8">
    <title> {{ if (isset .Params "shorttitle") }} {{ .Params.shorttitle }} {{ else }} {{ .Title }} {{ end }} </title>
    <base href="{{ .Site.BaseURL }}">

    <!-- Robots -->
    <meta name="title" content="{{ .Title }}">
    <meta name="description" content="{{ .Title }}">
    <meta name='date' content='{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}'>
    <meta name='revised' content='{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}'>

    <!-- Humans -->
    <meta name="author" content="{{ .Params.Author }}">
    <meta name='language' content='EN'>
    <meta name='robots' content='index,follow'>
    <meta name="HandheldFriendly" content="True">
    <meta name="MobileOptimized" content="320">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="icon" href="/img/faviconDIS-32px.gif" type="image/gif" sizes="32x32">
    {{ partial "css.html" }}
<body lang="en">

As you can see I tried to make it as simple as possible. I don,t show you the css because I’m not very proud of it.

Oops the summary file got mangled. Here it is:

<article class="docs">
    <header class="sum-title">
        <h3><a href='{{ .RelPermalink }}'> {{ .Title }}</a> </h3>
        <div class="sum-meta">{{ .Params.author}} - {{ .Date.Format "Mon, Jan 2, 2006" }} </div>
    <div class="sum-content"> {{ .Summary }} </div>
    {{ if .Truncated }} <footer>
        <a href="{{ .RelPermalink }}" class="sum-read-more">Read&nbsp;More…</a>
    </footer>{{ end }}

Using the code examples you shared, I tested it and it works as expected: they sort by weight. I am using 0.29.

I doubt there was a different in 0.28, but it was only the latest release for like a day, so maybe something broke. Try upgrading and seeing if that helps. :slight_smile:

If you want, you can send me the theme or templates, and I can test them directly. To really make sure, you might want to make a copy of the site site, clear out all the content, and try adding a few pieces from scratch, with only title, date and weight, to see how it is behaving; hopefully that reveals an obvious fix.

I just installed 0.29 and copied config.toml, layouts and static into a new directory. I created fresh content with the matter set with title date and weight. The files a displayed in creation order, ignoring the weight value.

Note that I created the directories by hand and copied the files needed. Note also that I’m on Linux (Ubuntu). I don’t know if this matter.

Activating verbose log, These are the warnings I see

  • INFO 2017/09/30 12:36:57 found taxonomies: map[string]string{“tag”:“tags”, “category”:“categories”}
  • WARN 2017/09/30 12:36:57 [en] Unable to locate layout for “taxonomyTerm”: [taxonomy/category.terms.en.html.html …
  • WARN 2017/09/30 12:36:57 [en] Unable to locate layout for “taxonomyTerm”: [taxonomy/tag.terms.en.html.html …
  • WARN 2017/09/30 12:36:57 [en] Unable to locate layout for “section”: [section/docs.en.html.html …
  • WARN 2017/09/30 12:36:57 [en] Unable to locate layout for “404”: [404.html] …
  • WARN 2017/09/30 12:36:57 Skip dataDir: lstat /home/xxx/Sources/test-www.ditp.dir/data: no such file or directory
  • WARN 2017/09/30 12:36:57 Skip i18nDir: lstat /home/xxx/Sources/test-www.ditp.dir/i18n: no such file or directory

How did you create your site ? What is your OS ?

Here is a public repository with the most simple code example illustrating the problem.

The site was created with Hugo 0.29 “hugo new site hugo-example”.
This is the HTML output:

<!DOCTYPE html>
	<meta name="generator" content="Hugo 0.29" />
    <meta charset="utf-8">
    <title> My New Hugo Site </title>

<p>Index title page</p> 


<script data-no-instant>document.write('<script src="/livereload.js?port=1313&mindelay=10"></' + 'script>')</script></body>

As you can see the pages are not sorted by weight. The order should by file0, file2, file1.
If you get something different, then it could be OS related. I don’t know.

I think your confusion is around a weight of 0. There was an issue about zero weight.

I am guessing as it is the default, pages assigned zero all fall in the same space, so any weighted piece gets processed first. Or something. I don’t know, we ought to pin that down and document it.

In the meantime, if you use any other weight, it sorts as expected. In my test I didn’t use 0, so I never ran into your issue. Now that you know, I guess you will figure out how to proceed. :slight_smile:

Excellent ! Thank you very very much for the help. That was indeed the error.

This is the documentation page that should be fixed. https://gohugo.io/content-management/front-matter/ Adding to the weight parameter value that it must be bigger than 0 would save a lot of time to people like me :slight_smile:

1 Like