How to grouping by categories

I want to create categories headline in index page.

For example,

<div>golang: </div>
<ul>
<li>2014-01-01 golang post 1</li>
<li>2012-01-01 golang post 2</li>
<li>2010-01-01 golang post 3</li>
</ul>

<div>html: </div>
<ul>
<li>2014-01-01 html post 1</li>
<li>2012-01-01 html post 2</li>
<li>2010-01-01 html post 3</li>
</ul>

The all post set categories like this.

---
layout: post
title: golang post 1
date: 2015-02-11
comments: true
categories: ["golang",]
---
content

I tried GroupByParam in index pages like this code.

<ulclass="params">
    {{ range .Site.Data.Pages.GroupByParam "categories" }}
    {{ .Title }}
    {{ end }}
</ul>

But no post displayed.

How I get posts group by categories?

(I tried other way, but I can’t do well. see first reply)

1 Like

I tried other way.
I set categories name to .Site.Params.

[params]
  categories = ["golang", "html"] 

I try iterate it’s list and I get post with a specific categories by where method.
So I write this code, but it don’t work because this error message.

<div id="params">
  {{ $site := . }}
  {{ range .Site.Params.categories }}
    <div class="category_name">{{ . }}</div>

    <ul class="posts">
        {{ range where $site.Data.Pages .Params.tags "in" "golang" }}

        <li>{{ .Title }}</li>
        {{ end }}
    </ul>
  {{ end }}
</div>
ERROR: 2015/03/02 Error while rendering homepage: template: theme/index.html:28:
59: executing "theme/index.html" at <.Params.tags>: can't evaluate field Params
in type interface {}

But this code work well and no post displayed

<div id="params">
{{ range where .Data.Pages .Params.tags "in" "golang" }}
<div>where tags {{ .Title }} {{ .Params.tags }} </div>
{{ end }}
</div>

I think $site.Data.Pages in upper case is equal to .Data.Pages in lowre case.
But it isn’t same result, and lower case doesn’t work.

It’s double Dutch to me.

Have you tried $.Site.Data.Pages?

I tried this code, but I got error.

            <div class="params3">
                param3<br />
                {{ range .Site.Params.categories }}
                <div class="category_name">{{ . }}</div>

                <ul class="posts">
                    {{ range where $.Site.Data.Pages .Params.categories "in" "golang" }}

                    <li>{{ .Title }}</li>
                    {{ end }}
                </ul>
                {{ end }}
                param3 end<br />
            </div>

error message

ERROR: 2015/03/03 Error while rendering homepage: template: theme/index.html:40:
60: executing "theme/index.html" at <.Params.categories>: can't evaluate field P
arams in type interface {}

In the soruce, is genereated, but no

  • and tag.

    I changed .Params.categories to .Params.tags, but I got same error…
    (I set same value to tags and categories)

  • Which version of Hugo are you running? This works in v0.13.

    $ cat layouts/index.html 
    <section>
      {{ range $taxonomyname, $taxonomy := .Site.Taxonomies }}
        {{ if eq "categories" $taxonomyname }}
          {{ range $key, $value := $taxonomy }}
            <div>{{ $key }}</div>
            <ul>
            {{ range $value.Pages }}
              <li>{{ .Date }} {{ .Title }}</li>
            {{ end }}
            </ul>
          {{ end }}
        {{ end }}
      {{ end }}
    </section>
    

    Results:

    $ hugo && cat public/index.html 
    0 draft content 
    0 future content 
    4 pages created 
    0 paginator pages created 
    0 tags created
    2 categories created
    0 series created
    in 8 ms
    <section>
            <div>golang</div>
            <ul>
              <li>2015-03-02 19:09:18 -0600 CST golang post 2</li>
              <li>2015-03-02 19:09:12 -0600 CST golang post 1</li>
            </ul>
          
            <div>html</div>
            <ul>
              <li>2015-03-02 19:09:36 -0600 CST html post 1</li>
              <li>2015-03-02 19:09:25 -0600 CST html post 2</li>
            </ul>
    </section>
    
    5 Likes

    The code’s output is exactly what I intended.
    I really appreciate it !!

    I didn’t understand the “.Site.Taxonomies”… thanks !


    I use hugo windows and mac, and these version is this.

    mac
    ./hugo_0.13_darwin_amd64 version
    Hugo Static Site Generator v0.13 BuildDate: 2015-02-22T04:02:30+09:00

    windows
    ./hugo_0.13_windows_amd64.exe version
    Hugo Static Site Generator v0.13 BuildDate: 2015-02-23T21:59:02+09:00