Adding Latest post and most popular tags to the sidebar

Hi all,

Trying to add to the sidewide sidebar a list with “Latest posts” and another one with
the most popular tags (limited to 20 tags) but can’t achieve this for some reason.

For the list with the “Latest post” I use the code below and it works on the home page
but on the internal “post” pages it doesn’t render anything:

        <h4>Latest</h4>
        <ul>
            {{ range first 5 .Data.Pages }}
            {{if eq .Type "post" }}
            <li><a href="{{ .RelPermalink }}">{{ .Title }}</a>, {{ .Date.Format "02 Jan, 2006" }}</li>
            {{ end }}
            {{ end }}
        </ul>

I have just one li.html in the /layout/default/ directory but I am not sure if this could be the problem?

About the “Tags” … I can’t render them at all even on the home page (but I have tags working on the “post” pages!).
Any hints will be appreciated.

This is the architecture of the project:

project-hugo2
├── archetypes
│   └── default.md
├── config.toml
├── content
│   ├── post
│   │   ├── post1.md
│   │   ├── post2.md
│   │   └── post3.md
│   └── page1.md
├── layouts
│   ├── 404.html
│   ├── _default
│   │   ├── li.html
│   │   ├── section.html
│   │   ├── single.html
│   │   └── summary.html
│   ├── index.html
│   ├── partials
│   │   ├── details.html
│   │   ├── disqus.html
│   │   ├── footer.html
│   │   ├── header.html
│   │   ├── meta_aside.html
│   │   ├── nav.html
│   │   ├── scripts.html
│   │   └── sidebar.html
│   ├── post
│   │   └── summary.html
│   ├── shortcodes
│   │   ├── img.html
│   │   └── youtube.html
│   └── taxonomy
│       ├── category.html
│       └── tag.html
├── public
├── src
└── static
    ├── css
    ├── images

Some progress here … I managed to display all the “Tags” on the sidebar with this:

<ul id="all-tags">
  {{ range $name, $taxonomy := .Site.Taxonomies.tags }}
    <li><a href="/tags/{{ $name | urlize }}">{{ $name }}</a></li>
  {{ end }}
</ul>

Looking for an option to limit the tags to 10 only (i.e. 10 most popular).

No progress with the “Latest posts” yet.

The first case, does it fail when it renders each posts, post1.md, post2.md…? If so, those are rendered with single.html and Page object which is passed as the top level object so to access .Data.Pages, you should use .Site.Pages instead of that.

The second case, I think http://gohugo.io/taxonomies/ordering/ helps you. Using .Site.Taxonomies.ByCount and first together seem to satisfy your needs.

Thank you @tatsushid

Tried this:

<ul>
    {{ range first 5 .Site.Pages }}
    {{if eq .Type "post" }}
    <li><a href="{{ .RelPermalink }}">{{ .Title }}</a>, {{ .Date.Format "02 Jan, 2006" }}</li>
    {{ end }}
    {{ end }}
</ul> 

but it doesn’t look to work (it renders zero info).

Will try with .Site.Taxonomies.ByCount for the tags now. :slight_smile:

What version’s hugo do you use? If 0.12 stable release, try .Site.Recent instead of .Site.Pages. In my case, I confirmed it worked when I wrote the template code in layout/post/single.html. In your case, it would fall back to layout/_default/single.html and posts are rendered with it. If you still fail, could you tell me where you wrote the code and on which page (url) you had a problem?

1 Like

Hi @tatsushid,

Great suggestion - it works perfectly. :slight_smile: Thank you!

By any chance any working example with the “Tags” list limited to 10 most used? :slight_smile:
I am using 0.12 stable.

Here is the example of the tag list.

<ul>
  {{ range first 10 .Site.Taxonomies.tags.ByCount }}
    <li><a href="/tags/{{ .Name | urlize }}">{{ .Name }}</a> ({{ .Count }})</li>
  {{ end }}
</ul>

On my environment, it works

1 Like

This is just fantastic. :slight_smile: Thanks a lot @tatsushid
Exactly what I needed.

Thank you once again!