Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-03T21:16:29.927Z
Answers.Text: Works! Thanks, I thought I was going crazy. Is there are reason putting it before the `{% children %}` tag works?
Answers.UserDisplayName: Alex Roper
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-03T21:19:10.253Z
Answers.Text: `{% children %}` is where things start to get recursive behind the scenes so variable scoping comes into play and `topLevelIndex` never gets updated.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-03T21:08:36.433Z
Answers.LastActivityDate: 2015-02-03T21:08:36.433Z
Answers.UserDisplayName: Brad Bell
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-03T12:33:16.710Z
Answers.Text: 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.
Answers.UserDisplayName: Alex Roper
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-03T12:38:47.950Z
Answers.Text: 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.
Answers.UserDisplayName: Alex Roper
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-03T12:51:24.207Z
Answers.Text: Aha. Maybe someone from P&T can explain that better for us :)
Answers.UserDisplayName: naboovalley
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-03T12:22:32.610Z
Answers.LastActivityDate: 2015-02-03T12:22:32.610Z
Answers.UserDisplayName: naboovalley
Body: <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>
  
Tags: <categories>
Title: Find the nth top-level category in a nav tag
ThreadText: # 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') %}
    <li>
      <a href="{{ category.url }}">{{ category.title }}</a>
      {% ifchildren %}
        <ul>
          {% children %}
        </ul>
      {% endifchildren %}
    </li>
    {% if category.level == 1 and loop.index == 10 %}
      <li>Zoiks! That‘s a lot of categories!</li>
    {% 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') %}
      <li>
  
          <a href="{{ category.url }}">{{ category.title }}</a>
  
          {% if category.level == 1 and loop.index == 10 %}
              <li>Zoiks! That‘s a lot of categories!</li>
          {% endif %}
  
          {% if category.level == 1 %}
              {% set topLevelIndex = topLevelIndex + 1 %}
          {% endif %}
  
          {% ifchildren %}
              <ul>
                  {% children %}
              </ul>
          {% endifchildren %}
      </li>
  {% 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 %}
      <li>Zoiks! That‘s a lot of categories!</li>
  {% 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 :)  
  
  ---
  
  