<p>You need to add <code>.first()</code> when setting the category. As far as I can tell, if you don't have <code>.first()</code> only <code>{{ category.slug }}</code> will work.</p>

<p>First get the slug:
</p>

<pre class="lang-twig prettyprint-override"><code>{% set catSlug = craft.request.getSegment(3) %}
</code></pre>

<p>This will give you the <a href="http://buildwithcraft.com/docs/templating/categorymodel">CategoryModel</a> object for catSlug:</p>

<pre class="lang-twig prettyprint-override"><code>{% set category = craft.categories.slug(catSlug).first() %}
</code></pre>

<p>You can now access whatever you want from the category, like the title:</p>

<pre class="lang-twig prettyprint-override"><code>{{ category.title }}
</code></pre>

CC BY-SA 3.0
2014-07-30T11:36:34.367Z
You can’t pass in a string (`catSlug`) to `craft.categories()` – that `.slug(` is required to tell Craft that what you’re passing in is the slug, as opposed to an ID, etc.
Brandon Kelly
CC BY-SA 3.0
2014-07-30T18:36:01.177Z
@Brandon Required as in it won't work without it or as in you really should use it? This code worked for me, maybe my test wasn't in a clean enough environment.
Simon Kuran
CC BY-SA 3.0
2014-07-30T22:08:55.863Z
Required as in there’s no way it will work that way, and if it appeared to work, you probably just got lucky with the desired category happening to be the first one in the group.
Brandon Kelly
CC BY-SA 3.0
2014-07-30T23:01:42.827Z
@Brandon You got it, I did get lucky. :) I edited my answer, the lack of `.first()` is what was giving @brunam problems.
Simon Kuran
CC BY-SA 3.0
2014-07-30T23:45:25.333Z
Ah good catch! That explains why `category.slug` was working but `category.title` wasn’t – it was just returning the parameter value, rather than an actual category’s slug.
Brandon Kelly
CC BY-SA 3.0
2014-07-29T23:43:59.747Z
2014-07-30T23:03:01.710Z
2014-07-30T23:03:01.710Z
Simon Kuran
<p>I'm setting my categories by matching the third segment to a category slug. How do I get the category title?</p>

<pre><code>{% set catSlug = craft.request.getSegment(3) %}
{% set category = craft.categories.slug(catSlug) %}
</code></pre>

<p>{{ category.slug }} returns the slug, but {{ category.title }} returns nothing.</p>

CC BY-SA 3.0
2014-07-30T11:39:11.220Z
If `{{ category.slug }}` is giving you the slug, it sounds like you’ve got is actually working. Not sure why `{{ category.title }}` would not be working though. What does `{{ dump(category) }}` give you? (you’ll need to have [dev mode](http://buildwithcraft.com/docs/config-settings#devMode) enabled to use that `dump()` function.)
Brandon Kelly
CC BY-SA 3.0
2014-07-30T12:14:05.827Z
Hmm looks right to me? Could be the category is disabled?
Keith Mancuso
<categories><slug><getsegment>
Get category title by slug
# Get category title by slug

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

I'm setting my categories by matching the third segment to a category slug. How do I get the category title?
{% set catSlug = craft.request.getSegment(3) %}
{% set category = craft.categories.slug(catSlug) %}

{{ category.slug }} returns the slug, but {{ category.title }} returns nothing.

**Tags:** categories, slug, getsegment
> **Comments:**
> **username_1**: If `{{ category.slug }}` is giving you the slug, it sounds like you’ve got is actually working. Not sure why `{{ category.title }}` would not be working though. What does `{{ dump(category) }}` give you? (you’ll need to have [dev mode](http://buildwithcraft.com/docs/config-settings#devMode) enabled to use that `dump()` function.)  
> **username_2**: Hmm looks right to me? Could be the category is disabled?  

---

## 1 answers

### Answer by username_3 on 2014-07-29. Score: 24 | Accepted answer

You need to add .first() when setting the category. As far as I can tell, if you don't have .first() only {{ category.slug }} will work.
First get the slug:

{% set catSlug = craft.request.getSegment(3) %}

This will give you the CategoryModel object for catSlug:
{% set category = craft.categories.slug(catSlug).first() %}

You can now access whatever you want from the category, like the title:
{{ category.title }}

> **Comments:**
> **username_1**: You can’t pass in a string (`catSlug`) to `craft.categories()` – that `.slug(` is required to tell Craft that what you’re passing in is the slug, as opposed to an ID, etc.  
> **username_3**: @Brandon Required as in it won't work without it or as in you really should use it? This code worked for me, maybe my test wasn't in a clean enough environment.  
> **username_1**: Required as in there’s no way it will work that way, and if it appeared to work, you probably just got lucky with the desired category happening to be the first one in the group.  
> **username_3**: @Brandon You got it, I did get lucky. :) I edited my answer, the lack of `.first()` is what was giving @brunam problems.  
> **username_1**: Ah good catch! That explains why `category.slug` was working but `category.title` wasn’t – it was just returning the parameter value, rather than an actual category’s slug.  

---

