Tip: How To Use The Zola Taxonomy Functions

Not sure how to post Tips or Hints for others, but we were using the Zola functions for taxonomy and because we don’t know rust that well or the tera template language that well.

If you want to build a dynamic menu of your taxonomies then this code might help you.

We ended up learning how to parse the config taxonomy data, to run through all the taxonomy ‘kinds’ and then we found you have to skip the first item as its not an irritable collection, which made things hard for our non coder template writer. anyways code says it best:

{# Keep as an example of how to drill down through taxonomy levels
{% for tax in config.taxonomies %}
  Taxonomy Name: {{tax.name}} <br>
  {% set categories = get_taxonomy(kind=tax.name) %}
  {% for name, items in categories %}
    {% set length = items | length %}
    {% if length > 0 %}
      Total items: {{length}}
      {% for item in items %}
        {% set url = get_taxonomy_url(kind=tax.name, name=item.name) %}
        {{url}} <br>
      {% endfor %}
    {% else %}
      First Item: {{items.name}}
    {% endif %}
  {% endfor %}
{% endfor %}

Our config.toml file looks like this:

taxonomies = [
    {name = "tags", rss = false, paginate_by = 5}, 
    {name = "english", rss = false, paginate_by = 5},  
    {name = "authors", rss = false, paginate_by = 5}, 
    {name = "downloads", rss = true, paginate_by = 8},
    {name = "podcast", rss = true, paginate_by = 75}, 

Note: If you have a lot of tags (we have 100+) and do this processing for every page (like in a site menu) then the build process will slow down dramatically.

If you know a better way or cleaner way please share :slight_smile:

1 Like

That’s because get_taxonomy also pulls all the pages of that taxonomy including the full text, which can be quite a lot. I’m not sure how to improve that though other than adding a param to not fetch the body.