<p>I have 4 level product categories. For the menu I am building I need to pick a second category in all top level categories</p>
<p>So I get top level categories like so</p>
<pre><code>{% set lv1Categories  = craft.categories.group('products').level(1) %}
</code></pre>
<p>How do I then get a second in the returned list?</p>
<pre><code>{% set cat = lv1Categories[1] %}
</code></pre>
<p>This doesn't seem to work</p>

CC BY-SA 4.0
2021-01-15T19:47:37.117Z
Hey, what do you expect `lv1Categories[1]` to do? Do you want a 2nd level subcategory?
Johannes
CC BY-SA 4.0
2021-01-15T19:52:03.280Z
Might help to post a screen cap of your category structure?
RitterKnight
CC BY-SA 4.0
2021-01-15T20:26:25.070Z
@Johannes There are two top level categories 'Recipients' and 'Departments'. 

I would expect lv1Categories[1] to reference 'Departments', so I can iterate over it's children with 

{% for categoryLv2 in categoryLv1.getChildren() %}
Lumiere
CC BY-SA 4.0
2021-01-18T08:36:50.550Z
If that's your actual markup, then the first line is missing a `.all()` - without it, you have a CategoryQuery object rather than an iterable set of results.
Martin Spain
<categories><craft2>
How to get a specific category by index
# How to get a specific category by index

(Asked by: username_0 on 2021-01-15)

I have 4 level product categories. For the menu I am building I need to pick a second category in all top level categories
So I get top level categories like so
{% set lv1Categories  = craft.categories.group('products').level(1) %}

How do I then get a second in the returned list?
{% set cat = lv1Categories[1] %}

This doesn't seem to work

**Tags:** categories, craft2
> **Comments:**
> **username_1**: Hey, what do you expect `lv1Categories[1]` to do? Do you want a 2nd level subcategory?  
> **username_2**: Might help to post a screen cap of your category structure?  
> **username_0**: @Johannes There are two top level categories 'Recipients' and 'Departments'.   I would expect lv1Categories[1] to reference 'Departments', so I can iterate over it's children with   {% for categoryLv2 in categoryLv1.getChildren() %}  
> **username_3**: If that's your actual markup, then the first line is missing a `.all()` - without it, you have a CategoryQuery object rather than an iterable set of results.  

---

## 0 answers

