<p>The <code>craft.categories</code> tag returns an <code>ElementCriteriaModel</code>, so <code>parentCategory.title</code> really shouldn't work. Try changing the first line of code to:</p>

<pre><code>{% set parentCategory = craft.categories.slug(craft.request.getSegment(2)).first() %}
</code></pre>

CC BY-SA 3.0
2014-10-29T17:53:00.303Z
That's what I've been trying, but then I get the error: Craft\CategoryModel and its behaviors do not have a method or closure named "first".

Have edited my question to show that.
Clive Portman
CC BY-SA 3.0
2014-10-30T09:01:36.987Z
That is very weird indeed. Are you sure craft.request.getSegment(2) returns a valid slug? Have you tried hardcoding a known slug into the parameter instead?
André Elvan
CC BY-SA 3.0
2014-10-30T16:09:10.033Z
Yep. Definitely does.
Clive Portman
CC BY-SA 3.0
2014-11-01T19:56:32.173Z
@ClivePortman You’ve tried that exact line of code? That definitely should work.
Brandon Kelly
CC BY-SA 3.0
2014-11-05T15:33:05.910Z
@BrandonKelly Definitely. I honestly can't add .first() to line 2 of my template, as described above, without the error. I can on the next line, but not if I remove line 2.
Clive Portman
CC BY-SA 3.0
2014-10-29T17:34:35.707Z
2014-10-30T16:10:04.863Z
2014-10-30T16:10:04.863Z
André Elvan
<p>If this is coming from a category’s primary URL – so a <code>category</code> variable is already defined for you – another way to get that category’s parent is by using the CategoryModel’s <a href="http://buildwithcraft.com/docs/templating/categorymodel#getParent" rel="nofollow">getParent()</a> function:</p>

<pre class="lang-twig prettyprint-override"><code>{% set parentCategory = category.getParent() %}
</code></pre>

CC BY-SA 3.0
2014-11-01T20:00:18.617Z
2014-11-01T20:00:18.617Z
Brandon Kelly
<p>So, in my template I get what I'm calling parentCategory, and I can output what I need:</p>

<pre><code>{% set parentCategory = craft.categories.slug(craft.request.getSegment(2)) %}
{# parentCategory.title and parentCategory.slug output fine #}
</code></pre>

<p>But shortly after, I'm wanting to get all the descendants of parentCategory, like so:</p>

<pre><code>{% set cats = craft.categories.descendantOf(parentCategory.id).find() %}
{# outputs all categories, whether descendants or not #}
</code></pre>

<p>It seems parentCategory.id is empty:</p>

<pre><code>{{ parentCategory.id }}
{# outputs nothing #}
</code></pre>

<p>Any ideas where I'm going wrong here? Why is no ID being returned while title, slug and my custom fields are working fine?</p>

<p>Here's line 1 and 2 from my template:</p>

<pre><code>{% if craft.request.getSegment(3) %}
    {% set parentCategory = craft.categories.slug(craft.request.getSegment(2)) %}
</code></pre>

<p>If I change line 2 to:
{% set parentCategory = craft.categories.slug(craft.request.getSegment(2)).first() %}</p>

<p>I get the following error:</p>

<pre><code>Craft\CategoryModel and its behaviors do not have a method or closure named "first".
</code></pre>

<p>Yet, I can repeat line 2 later in the template and include .first() and not get the error. What go on?</p>

<categories>
Category ID not being returned
# Category ID not being returned

(Asked by: username_0 on 2014-10-29)

So, in my template I get what I'm calling parentCategory, and I can output what I need:
{% set parentCategory = craft.categories.slug(craft.request.getSegment(2)) %}
{# parentCategory.title and parentCategory.slug output fine #}

But shortly after, I'm wanting to get all the descendants of parentCategory, like so:
{% set cats = craft.categories.descendantOf(parentCategory.id).find() %}
{# outputs all categories, whether descendants or not #}

It seems parentCategory.id is empty:
{{ parentCategory.id }}
{# outputs nothing #}

Any ideas where I'm going wrong here? Why is no ID being returned while title, slug and my custom fields are working fine?
Here's line 1 and 2 from my template:
{% if craft.request.getSegment(3) %}
    {% set parentCategory = craft.categories.slug(craft.request.getSegment(2)) %}

If I change line 2 to:
{% set parentCategory = craft.categories.slug(craft.request.getSegment(2)).first() %}
I get the following error:
Craft\CategoryModel and its behaviors do not have a method or closure named "first".

Yet, I can repeat line 2 later in the template and include .first() and not get the error. What go on?

**Tags:** categories

---

## 2 answers

### Answer by username_1 on 2014-10-29. Score: 3

The craft.categories tag returns an ElementCriteriaModel, so parentCategory.title really shouldn't work. Try changing the first line of code to:
{% set parentCategory = craft.categories.slug(craft.request.getSegment(2)).first() %}

> **Comments:**
> **username_0**: That's what I've been trying, but then I get the error: Craft\CategoryModel and its behaviors do not have a method or closure named "first".  Have edited my question to show that.  
> **username_1**: That is very weird indeed. Are you sure craft.request.getSegment(2) returns a valid slug? Have you tried hardcoding a known slug into the parameter instead?  
> **username_0**: Yep. Definitely does.  
> **username_2**: @ClivePortman You’ve tried that exact line of code? That definitely should work.  
> **username_0**: @BrandonKelly Definitely. I honestly can't add .first() to line 2 of my template, as described above, without the error. I can on the next line, but not if I remove line 2.  

---

### Answer by username_2 on 2014-11-01. Score: 0

If this is coming from a category’s primary URL – so a category variable is already defined for you – another way to get that category’s parent is by using the CategoryModel’s getParent() function:
{% set parentCategory = category.getParent() %}


---

