A Menu Generated From a Taxonomy or Sections

Rather than creating a menu in the config file or hard coded I’d like to be able to generate it from all the unique terms of a single taxonomy. Say for example there is a taxonomy “catageories”. Does Hugo have a method/structure for the collection of unique terms in a taxonomy (across all content) or do I need to walk through .Data.Pages and .Params.categories and build it myself? Without a context (content root) how does one walk through ALL the .Data.Pages?

The ultimate purpose of this is to make an easy way for a noob to have menus based on a taxonomy’s terms. If they add a new term in some content’s frontmatter they get a new menu item.

I suppose alternatively .Site.Sections could be used to do something equivalent? Is .Site.Sections only from the content folder (documentation is unclear)? How does one access the individual sections? .Name doesn’t.

Bits of code in your reply is appreciated.

I’m mostly happy with sections as the menu items, so I wrote this:

1 Like

But I also use taxonomies on a site:


The down-side of this is that I don’t get the built-in IsMenuCurrent and HasMenuCurrent.

@bjornerik has your section menu code been merged into the master yet?

Still pondering the second post. If I read it right this is the part doing the “extracting” of unique terms. My idea though was to populate a menu instance with this information rather than just create a styled list of links then that can be used like any menu built in config or frontmatter. Is that possible with just mustache codes? or must be done in Go like the section menu code? Ultimately a simple entry in config like you did for sections would be great with a parameter for index/taxonomy name which would also be used for the menu name. In general sections should work fine for most unless it made more sense to have section structure hidden from viewers and use a taxonomy to organize the site for viewers. Also taxonomy based menu would allow “double posting” within the menu system which might be useful in some cases.

Glad to discover others have thought of this especially ones who can do somthing about it! Thanks @bjornerik
I’d be glad to do my small part and add this to the documention for the next realease.

The “magic section menu” is still i PR mode.

And I think it could be fruitful to think further on this into taxonomy land, but I’m not sure how it would look. Menus are a little too much manual work at the moment.

For the sake of discussion, I can explain my “Section menu for lazy bloggers”:

  • If a menu name is set, the sections will be put in a menu with that name
  • ¤¤¤ the pages in those sections will behave as “shadow members” of that menu item, i.e. answer truthful to HasMenuCurrent.

The menu items will get their identity and name set from the section name (“blog” -> identifier: “blog”, name: “Blog”), but no Weight (I guess alfab. sorting).

If you want custom name or weight, you can override it in config.toml with the section name as the identifier:

    name = "My Great Blog"
    weight = -120
    identifier = "blog"
    url = "/blog"

¤¤¤ This is an item where I may have thought too much about my own use cases: My sections tend to have many pages, and I don’t want to list them as childrens in my menu, I just want them to behave like they are – without having to add front matter config for each. But it is easy to add them as “full blown members”.