Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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 enabled to use that dump() function.) – Brandon Kelly Jul 30 '14 at 11:39
Hmm looks right to me? Could be the category is disabled? – Keith Mancuso Jul 30 '14 at 12:14
up vote 14 down vote accepted

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 }}
share|improve this answer
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 Jul 30 '14 at 11:36
@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 Jul 30 '14 at 18:36
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 Jul 30 '14 at 22:08
@Brandon You got it, I did get lucky. :) I edited my answer, the lack of .first() is what was giving @brunam problems. – Simon Kuran Jul 30 '14 at 23:01
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 Jul 30 '14 at 23:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.