Find the nth top-level category in a nav tag I want to output some code after the 10th top level category in a category listing. For example...


1.

1.1.
1.2.

2.

2.1.

3.
...
10.
Zoiks!


The problem is, I can't use Twig's loop.index in a conditional because it counts each of the children too (like a flat list). How can I test for the 10th top-level category?

{% nav category in craft.categories.group('categoryGroup') %}
  &lt;li&gt;
    &lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;
    {% ifchildren %}
      &lt;ul&gt;
        {% children %}
      &lt;/ul&gt;
    {% endifchildren %}
  &lt;/li&gt;
  {% if category.level == 1 and loop.index == 10 %}
    &lt;li&gt;Zoiks! That‘s a lot of categories!&lt;/li&gt;
  {% endif %}
{% endnav %}
Maybe you can add in a little counting magic of your own:

{# before the nav tag #}
{% set topLevelIndex = 1 %}

{# inside nav loop #}
{% if category.level == 1 and topLevelIndex == 10 %}
    &lt;li&gt;Zoiks! That‘s a lot of categories!&lt;/li&gt;
{% endif %}
{% if category.level == 1 %}
    {% set topLevelIndex = topLevelIndex + 1 %}
{% endif %}


This code hasn't been tested...