HUGO

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
gohugoio Star
  • About Hugo
    • Overview
    • What is Hugo
    • Hugo features
    • Static site generators
    • Hugo's security model
    • Hugo and the GDPR
    • License
  • Installation
    • Overview
    • macOS
    • Linux
    • Windows
    • BSD
  • Getting started
    • Overview
    • Quick start
    • Basic usage
    • Directory structure
    • Configuration
    • Configure markup
    • Glossary of terms
    • External learning resources
  • Quick reference
    • Overview
    • Emojis
    • Functions
    • Methods
    • Page collections
  • Content management
    • Overview
    • Organization
    • Page bundles
    • Content formats
    • Front matter
    • Build options
    • Page resources
    • Image processing
    • Shortcodes
    • Related content
    • Sections
    • Content types
    • Archetypes
    • Taxonomies
    • Summaries
    • Links and cross references
    • URL management
    • Menus
    • Static files
    • Table of contents
    • Comments
    • Multilingual
    • Markdown attributes
    • Syntax highlighting
    • Diagrams
    • Mathematics
  • Templates
    • Overview
    • Templating
    • Template lookup order
    • Base templates and blocks
    • Single page templates
    • List templates
    • Homepage template
    • Section templates
    • Taxonomy templates
    • Pagination
    • Content view templates
    • Partial templates
    • Shortcode templates
    • Menu templates
    • Data templates
    • RSS templates
    • Sitemap templates
    • Internal templates
    • Custom output formats
    • 404 page
    • Robots.txt
  • Functions
    • Overview
    • cast
    • collections
    • compare
    • crypto
    • data
    • debug
    • diagrams
    • encoding
    • fmt
    • global
    • go template
    • hugo
    • images
    • inflect
    • js
    • lang
    • math
    • openapi3
    • os
    • partials
    • path
    • reflect
    • resources
    • safe
    • strings
    • templates
    • time
    • transform
    • urls
  • Methods
    • Overview
    • Duration
    • Menu
    • Menu entry
    • Page
    • Pages
    • Resource
    • Shortcode
    • Site
    • Taxonomy
    • Time
  • Render hooks
    • Overview
    • Introduction
    • Code blocks
    • Headings
    • Images
    • Links
  • Hugo Modules
    • Overview
    • Configure Hugo modules
    • Use Hugo Modules
    • Theme components
  • Hugo Pipes
    • Overview
    • Introduction
    • Transpile Sass to CSS
    • PostCSS
    • PostProcess
    • JavaScript building
    • Babel
    • Asset minification
    • Concatenating assets
    • Fingerprinting and SRI hashing
    • Resource from string
    • Resource from template
  • CLI
  • Troubleshooting
    • Overview
    • Audit
    • Logging
    • Inspection
    • Deprecation
    • Performance
    • FAQs
  • Developer tools
    • Overview
    • Editor plugins
    • Front-ends
    • Search
    • Migrations
    • Other projects
  • Hosting and deployment
    • Overview
    • Hugo Deploy
    • Deploy with Rclone
    • Deploy with Rsync
    • Host on 21YunBox
    • Host on AWS Amplify
    • Host on Azure Static Web Apps
    • Host on Cloudflare Pages
    • Host on Firebase
    • Host on GitHub Pages
    • Host on GitLab Pages
    • Host on KeyCDN
    • Host on Netlify
    • Host on Render
  • Contribute
    • Overview
    • Development
    • Documentation
    • Themes
  • Maintenance
METHODS TAXONOMY METHODS

ByCount

Returns an ordered taxonomy, sorted by the number of pages associated with each term.

Syntax

TAXONOMY.ByCount

Returns

page.OrderedTaxonomy

The ByCount method on a Taxonomy object returns an ordered taxonomy, sorted by the number of pages associated with each term.

While a Taxonomy object is a map, an ordered taxonomy is a slice, where each element is an object that contains the term and a slice of its weighted pages.

Before we can use a Taxonomy method, we need to capture a Taxonomy object.

Capture a taxonomy object

Consider this site configuration:

hugo.
     
taxonomies:
  author: authors
  genre: genres
[taxonomies]
  author = 'authors'
  genre = 'genres'
{
   "taxonomies": {
      "author": "authors",
      "genre": "genres"
   }
}

And this content structure:

content/
├── books/
│   ├── and-then-there-were-none.md --> genres: suspense
│   ├── death-on-the-nile.md        --> genres: suspense
│   └── jamaica-inn.md              --> genres: suspense, romance
│   └── pride-and-prejudice.md      --> genres: romance
└── _index.md

To capture the “genres” taxonomy object from within any template, use the Taxonomies method on a Site object.

{{ $taxonomyObject := .Site.Taxonomies.genres }}

To capture the “genres” taxonomy object when rendering its page with a taxonomy template, use the Terms method on the page’s Data object:

layouts/_default/taxonomy.html
{{ $taxonomyObject := .Data.Terms }}

To inspect the data structure:

<pre>{{ jsonify (dict "indent" "  ") $taxonomyObject }}</pre>

Although the Alphabetical and ByCount methods provide a better data structure for ranging through the taxonomy, you can render the weighted pages by term directly from the Taxonomy object:

{{ range $term, $weightedPages := $taxonomyObject }}
  <h2><a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a></h2>
  <ul>
    {{ range $weightedPages }}
      <li><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></li>
    {{ end }}
  </ul>
{{ end }}

In the example above, the first anchor element is a link to the term page.

Get the ordered taxonomy

Now that we have captured the “genres” Taxonomy object, let’s get the ordered taxonomy sorted by the number of pages associated with each term:

{{ $taxonomyObject.ByCount }}

To reverse the sort order:

{{ $taxonomyObject.ByCount.Reverse }}

To inspect the data structure:

<pre>{{ jsonify (dict "indent" "  ") $taxonomyObject.ByCount }}</pre>

An ordered taxonomy is a slice, where each element is an object that contains the term and a slice of its weighted pages.

Each element of the slice provides these methods:

Count
(int) Returns the number of pages to which the term is assigned.
Page
(page.Page) Returns the term’s Page object, useful for linking to the term page.
Pages
(page.Pages) Returns a Pages object containing the Page objects to which the term is assigned, sorted by taxonomic weight. To sort or group, use any of the methods available to the Pages object. For example, sort by the last modification date.
Term
(string) Returns the term name.
WeightedPages
(page.WeightedPages) Returns a slice of weighted pages to which the term is assigned, sorted by taxonomic weight. The Pages method above is more flexible, allowing you to sort and group.

Example

With this template:

{{ range $taxonomyObject.ByCount }}
  <h2><a href="{{ .Page.RelPermalink }}">{{ .Page.LinkTitle }}</a> ({{ .Count }})</h2>
  <ul>
    {{ range .Pages.ByTitle }}
      <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
    {{ end }}
  </ul>
{{ end }}

Hugo renders:

<h2><a href="/genres/suspense/">suspense</a> (3)</h2>
<ul>
  <li><a href="/books/and-then-there-were-none/">And then there were none</a></li>
  <li><a href="/books/death-on-the-nile/">Death on the nile</a></li>
  <li><a href="/books/jamaica-inn/">Jamaica inn</a></li>
</ul>
<h2><a href="/genres/romance/">romance</a> (2)</h2>
<ul>
  <li><a href="/books/jamaica-inn/">Jamaica inn</a></li>
  <li><a href="/books/pride-and-prejudice/">Pride and prejudice</a></li>
</ul>

See also

  • Alphabetical

On this page

  • Capture a taxonomy object
  • Get the ordered taxonomy
  • Example

In this section

  • Alphabetical
  • ByCount
  • Count
  • Get
Last updated: November 19, 2023: Use LinkTitle consistently in examples (5bccf8b1)
Improve this page
By the Hugo Authors
Hugo Logo
  • File an Issue
  • Get Help
  • @GoHugoIO
  • @spf13
  • @bepsays

Netlify badge

 

Hugo Sponsors

 

The Hugo logos are copyright © Steve Francia 2013–2024.

The Hugo Gopher is based on an original work by Renée French.

  • News
  • Docs
  • Themes
  • Showcase
  • Community
  • GitHub
  • About Hugo
  • Installation
  • Getting started
  • Quick reference
  • Content management
  • Templates
  • Functions
  • Methods
  • Render hooks
  • Hugo Modules
  • Hugo Pipes
  • CLI
  • Troubleshooting
  • Developer tools
  • Hosting and deployment
  • Contribute
  • Maintenance