<p>I think you'll just need to put it <em>before</em> the <code>{% children %}</code> tags.  Something like:</p>

<pre><code>{% nav category in craft.categories.group('categoryGroup') %}
    &lt;li&gt;

        &lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;

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

        {% if category.level == 1 %}
            {% set topLevelIndex = topLevelIndex + 1 %}
        {% endif %}

        {% ifchildren %}
            &lt;ul&gt;
                {% children %}
            &lt;/ul&gt;
        {% endifchildren %}
    &lt;/li&gt;
{% endnav %}
</code></pre>

CC BY-SA 3.0
2015-02-03T21:16:29.927Z
Works! Thanks, I thought I was going crazy. Is there are reason putting it before the `{% children %}` tag works?
Alex Roper
CC BY-SA 3.0
2015-02-03T21:19:10.253Z
`{% children %}` is where things start to get recursive behind the scenes so variable scoping comes into play and `topLevelIndex` never gets updated.
Brad Bell
CC BY-SA 3.0
2015-02-03T21:08:36.433Z
2015-02-03T21:08:36.433Z
Brad Bell
<p>Maybe you can add in a little counting magic of your own:</p>

<pre><code>{# 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 %}
</code></pre>

<p><em>This code hasn't been tested...</em></p>

CC BY-SA 3.0
2015-02-03T12:33:16.710Z
That's the workaround I tried initially, but topLevelIndex never goes past 2. I tested it by putting `{{ topLevelIndex }}` right after trying to increment it. I have `set topLevelIndex = 1` outside the nav tag and `set topLevelIndex = topLevelIndex + 1` is inside the nav tag loop.
Alex Roper
CC BY-SA 3.0
2015-02-03T12:38:47.950Z
I think this might be a bug with the nav tag. If I try the same counter method using a for loop instead of the nav tag, it works as expected.
Alex Roper
CC BY-SA 3.0
2015-02-03T12:51:24.207Z
Aha. Maybe someone from P&T can explain that better for us :)
naboovalley
CC BY-SA 3.0
2015-02-03T12:22:32.610Z
2015-02-03T12:22:32.610Z
naboovalley
<p>I want to output some code after the 10th <strong>top level</strong> category in a category listing. For example...</p>

<ul>
<li>1.
<ul>
<li>1.1.</li>
<li>1.2.</li>
</ul></li>
<li>2.
<ul>
<li>2.1.</li>
</ul></li>
<li>3.</li>
<li>...</li>
<li>10.</li>
<li>Zoiks!</li>
</ul>

<p>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?</p>

<pre><code>{% 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 %}
</code></pre>

<categories>
Find the nth top-level category in a nav tag
# Find the nth top-level category in a nav tag

(Asked by: username_0 on 2015-02-03)

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') %}
  
    {{ category.title }}
    {% ifchildren %}
      
        {% children %}
      
    {% endifchildren %}
  
  {% if category.level == 1 and loop.index == 10 %}
    Zoiks! That‘s a lot of categories!
  {% endif %}
{% endnav %}

**Tags:** categories

---

## 2 answers

### Answer by username_1 on 2015-02-03. Score: 2 | Accepted answer

I think you'll just need to put it before the {% children %} tags.  Something like:
{% nav category in craft.categories.group('categoryGroup') %}
    

        {{ category.title }}

        {% if category.level == 1 and loop.index == 10 %}
            Zoiks! That‘s a lot of categories!
        {% endif %}

        {% if category.level == 1 %}
            {% set topLevelIndex = topLevelIndex + 1 %}
        {% endif %}

        {% ifchildren %}
            
                {% children %}
            
        {% endifchildren %}
    
{% endnav %}

> **Comments:**
> **username_0**: Works! Thanks, I thought I was going crazy. Is there are reason putting it before the `{% children %}` tag works?  
> **username_1**: `{% children %}` is where things start to get recursive behind the scenes so variable scoping comes into play and `topLevelIndex` never gets updated.  

---

### Answer by username_2 on 2015-02-03. Score: 2

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 %}
    Zoiks! That‘s a lot of categories!
{% endif %}
{% if category.level == 1 %}
    {% set topLevelIndex = topLevelIndex + 1 %}
{% endif %}

This code hasn't been tested...

> **Comments:**
> **username_0**: That's the workaround I tried initially, but topLevelIndex never goes past 2. I tested it by putting `{{ topLevelIndex }}` right after trying to increment it. I have `set topLevelIndex = 1` outside the nav tag and `set topLevelIndex = topLevelIndex + 1` is inside the nav tag loop.  
> **username_0**: I think this might be a bug with the nav tag. If I try the same counter method using a for loop instead of the nav tag, it works as expected.  
> **username_2**: Aha. Maybe someone from P&T can explain that better for us :)  

---

