List only the first 15 categories

Hi Folks! I’m brand-new to Hugo but have lots of experience with tech in general and the web itself. I’m using Hugo to develop a brand-new website. I’ve only been working on it for five days now but a great deal of it is working great and looking good. Having been working as profession back in the 80s, the markdown language feels like an old friend and, of course, the CSS and HTML is clear and, at least in the theme I chose, really well ordered. It’s all good.

I’ve got one question I can’t figure out. I want to limit the list of categories (or tags) to just the first 15 items. I’ve been trying to use range to limit the list and the software gods are laughing at me because I’ve tried so many variations on a theme and all have resulted in a deadly error.

I’m pretty sure that my problem concerns scope but I don’t have enough experience with Hugo’s templating yet to figure it out.

If anyone has a snippet showing how to list just the first N numbers of items in one of the taxonomies, I’d be very grateful.

Thanks, everyone. :slight_smile:

Brian

https://gohugo.io/functions/first

Hi Joe! Thanks! I found the first function fairly early in my process and thought “Oh, this will be easy!” LOL

Here are two of the simpler versions I tried, both of which are very much unloved by the Hugo server:

{{- range first 10 site.Taxonomies.categories }}
<li><a href="{{ .Page.RelPermalink }}"{{ if (and (eq $.Page.Kind "term") (eq $.Page.Type "categories") (eq $.Page.Title .Page.Title)) }} class="current"{{ end }}>{{ .Page.Title }}</a></li>
{{- end }}


{{- range first 10 (site.Taxonomies.categories) }}
<li><a href="{{ .Page.RelPermalink }}"{{ if (and (eq $.Page.Kind "term") (eq $.Page.Type "categories") (eq $.Page.Title .Page.Title)) }} class="current"{{ end }}>{{ .Page.Title }}</a></li>
{{- end }}

Thanks again for replying. :slight_smile:

Brian

Here’s the error returned by the server for the second version.

Brian

What do you wish to appear on the page?
What is the path of the template?

The full path is

D:\Healthy\healthy_site\themes\airspace-hugo\layouts\partials\widgets\taxonomy_category.html

The list in the sidebar. We expect a lot of categories, so I just want to list the first 10. I’ve already created a link below the list that will take the reader to a page that lists all of the categories. That works fine.

What I’m hoping for is just a list of the first 10 categories. The default ordering is alphabetical, which is perfect.

Thanks again for your help. :slight_smile:
Brian

error calling first: can’t iterate over hugolib.Taxonomy

Yeah, that’s kind of annoying.

Pack them into a slice, then it works…

{{ $p := slice }}
{{ range site.Taxonomies.categories }}
  {{ $p = $p | append .Page }}
{{ end }}

{{ range first 10 $p }}
  <a href="{{ .Permalink }}">{{ .LinkTitle }}</a>
{{ end }}
3 Likes

Thanks so much, Joe!

I greatly appreciate your help. :slight_smile:

I hope you have a great weekend.

Brian

1 Like

More info…

{{ printf "%T" site.Taxonomies.categories }} --> hugolib.Taxonomy
{{ reflect.IsMap site.Taxonomies.categories }} --> true

A map is not ordered, so the concepts of first and last are nonsensical.

You can convert the unordered map into an ordered array by applying the .Alphabetical, or .ByCount methods.

{{ printf "%T" site.Taxonomies.categories.Alphabetical }} --> hugolib.OrderedTaxonomy
{{ reflect.IsMap site.Taxonomies.categories.Alphabetical }} --> false

{{ printf "%T" site.Taxonomies.categories.ByCount }} --> hugolib.OrderedTaxonomy
{{ reflect.IsMap site.Taxonomies.categories.ByCount }} --> false

So back to your original question, do this:

{{ range first 10 site.Taxonomies.categories.Alphabetical }}
  <a href="{{ .Page.Permalink }}">{{ .Page.LinkTitle }}</a>
{{ end }}
2 Likes

Joe, that additional information is extremely helpful to my understanding of Hugo.

Thanks very much for taking the time to add it. It really helps.

Brian

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.