Sorting site taxonomy

Hi - I’ve got the following in a 404.html and am using it for my 404 page. It works. Currently I have topics, tags and series defined, and it comes up listed as:

  1. Series
  2. Tags
  3. Topics

With the content interspersed. However, I’d like to sort the top-level taxonomies the reverse of how they sort now, in alphabetical order.

I tried a few things like adding .ByCount or .Alphabetical.Reverse to:

{{ range $taxonomyname, $taxonomy := .Site.Taxonomies.Alphabetical.Reverse }}

… but nothing seems to work. This is what the template looks like now.

{{ partial "header.html" . }}
<body>
    <div id="layout" class="pure-g">
        {{ partial "sidebar.html" . }} 
        {{ partial "hometop.html" . }}

        <div class="content pure-u-1 pure-u-md-3-4">
            <a name="top"></a> {{ partial "listtop_home.html" . }}

            <main>
                <div class="posts">
                    <section class="post">
                        <header class="post-header">
                            <h1 class="post-title">Oh Dear, it's the dreaded &laquo;{{ .Title }}&raquo;</h1>
                        </header>
                            <article class="post-summary">
                            Thanks for visiting. You've reached my 404 page, meaning the content you tried to access cannot be found. Use the site search feature, or scan the below list of site taxonomies and click through to something of interest. 
                            <ul>
                            {{ range $taxonomyname, $taxonomy := .Site.Taxonomies }}
                            <li><a href="/{{ $taxonomyname | urlize }}"><h2>{{ $taxonomyname }}</h2></a>
                                <ul>
                                {{ range $key, $value := $taxonomy }}
                                <li><h3>{{ $key }}</h3></li>
                                    <ul>
                                    {{ range $value.Pages }}
                                    <li hugo-nav="{{ .RelPermalink }}"><a href="{{ .Permalink}}"><em>{{ .LinkTitle }}</em></a></li>
                                    {{ end }}
                                    </ul>
                                {{ end }}
                                </ul>
                            </li>
                            {{ end }}
                            </ul>
                            </article>
                    </section>
                </div>
            </main>
            {{ partial "footer.html" . }}
        </div>
    </div>
    {{ partial "bodyend.html" . }}
</body>
</html>

How can I sort the top level terms in this case?

Thank you in advance.
–Rick

1 Like

wow, good luck for me

Hi - just wanted to also ask if anyone knows if the 404 in the root of the site should load automatically, or is it a usual requirement to add an .htaccess with this:

ErrorDocument 404 /404.html

When I said it was working above, I had been looking directly at the page, not entering erroneous URL paths.

When I tried http://mysite.com/blahblah, it came up with a default Apache handler, so I went ahead and handled loading this custom page with .htaccess.

I was just wondering what’s the convention, generally, or if Hugo is supposed to do this for us.

Hugo does nothing. The 404.html in the root folder was chosen beause it works out of the box on GitHub Pages.

hugo server doesn’t handle them at all (open issue), on Amazon S3 you have to set 404.html as the error file (404 is the only http error you will get) etc …

Ok, I think I’ll add a comment about that, to the docs.

And now merged.

Ok, I recompiled hugo with go get this AM, then tried adding:

{{ range $taxonomyname, $taxonomy := .Site.Taxonomies.Alphabetical.Reverse }}

It still does not seem to work, and I get no particular error in the console for hugo server, but, nothing renders either.

If I can understand it, I will add this example to the docs and send a PR.

I thinks you misunderstand this feature.

.Site.Taxonomies is a map with the taxonomy lists with no sorting (there may be ways to sort this, but not this way).

.Site.Taxonomies.tags.Alphabetical.Reverse is good to go.`

A strong likelihood! :slight_smile:

Sorry, I thought because you pasted that here, it was relevant to the problem I’d mentioned.

One approach for me, is to just separate the lists out. I can make a static link to each taxonomy, and then list up its contents under the link header.

Yes, that was my intention, but I read your post a little too quick.