<p>First you’ll need to grab the category ID from the query string, using <a href="http://buildwithcraft.com/docs/templating/craft.request#getQuery" rel="noreferrer">craft.request.getQuery()</a>:</p>

<pre><code>{% set catId = craft.request.getQuery('catId') %}
</code></pre>

<p>Then you can fetch the <a href="http://buildwithcraft.com/docs/templating/categorymodel" rel="noreferrer">CategoryModel</a> via <a href="http://buildwithcraft.com/docs/templating/craft.categories" rel="noreferrer">craft.categories</a> and its <a href="http://buildwithcraft.com/docs/templating/craft.categories#id" rel="noreferrer">id param</a>:</p>

<pre><code>{% set category = craft.categories.id(catId).first() %}
</code></pre>

<p>We can’t be sure that the ID was actually valid, so make sure that <code>category</code> is actually set to something, otherwise return a 404 error:</p>

<pre><code>{% if not category %}
    {% exit 404 %}
{% endif %}
</code></pre>

<p>Now, to output the category’s title, just do this:</p>

<pre><code>{{ category.title }}
</code></pre>

<p>There are a couple ways you can put this all together, depending on whether the page <strong>requires</strong> a category ID passed to it, or if it’s optional.</p>

<p>If it’s required, we should verify that the <code>catId</code> query parameter was actually set to something right off the bat:</p>

<pre><code>{% set catId = craft.request.getQuery('catId') %}

{# The catId param is required #}
{% if not catId %}
    {% exit 404 %}
{% endif %}

{% set category = craft.categories.id(catId).first() %}

{% if not category %}
    {% exit 404 %}
{% endif %}

{{ category.title }}
</code></pre>

<p>If it’s optional, then we should grab the ID from the query string, and then wrap all of the remaining logic in a conditional, so it only runs if there was an ID:</p>

<pre><code>{% set catId = craft.request.getQuery('catId') %}

{# The catId param is optional #}
{% if catId %}
    {% set category = craft.categories.id(catId).first() %}

    {% if not category %}
        {% exit 404 %}
    {% endif %}

    {{ category.title }}
{% endif %}
</code></pre>

CC BY-SA 3.0
2015-10-08T14:03:49.677Z
2015-10-08T14:03:49.677Z
Brandon Kelly
<p>I’m passing the ID of a category to a template via a <code>catId</code> query string (i.e. <a href="http://example.com/some/path?catId=10" rel="nofollow">http://example.com/some/path?catId=10</a>).</p>

<p>How do I display the title of that category?</p>

<categories><filter><category><titles>
How to display the title of a category based on its ID?
# How to display the title of a category based on its ID?

(Asked by: username_0 on 2015-10-08)

I’m passing the ID of a category to a template via a `catId` query string (i.e. <http://example.com/some/path?catId=10>).

How do I display the title of that category?

**Tags:** categories, filter, category, titles

---

## 1 answers

### Answer by username_1 on 2015-10-08. Score: 5 | Accepted answer

First you’ll need to grab the category ID from the query string, using craft.request.getQuery():
{% set catId = craft.request.getQuery('catId') %}

Then you can fetch the CategoryModel via craft.categories and its id param:
{% set category = craft.categories.id(catId).first() %}

We can’t be sure that the ID was actually valid, so make sure that category is actually set to something, otherwise return a 404 error:
{% if not category %}
    {% exit 404 %}
{% endif %}

Now, to output the category’s title, just do this:
{{ category.title }}

There are a couple ways you can put this all together, depending on whether the page requires a category ID passed to it, or if it’s optional.
If it’s required, we should verify that the catId query parameter was actually set to something right off the bat:
{% set catId = craft.request.getQuery('catId') %}

{# The catId param is required #}
{% if not catId %}
    {% exit 404 %}
{% endif %}

{% set category = craft.categories.id(catId).first() %}

{% if not category %}
    {% exit 404 %}
{% endif %}

{{ category.title }}

If it’s optional, then we should grab the ID from the query string, and then wrap all of the remaining logic in a conditional, so it only runs if there was an ID:
{% set catId = craft.request.getQuery('catId') %}

{# The catId param is optional #}
{% if catId %}
    {% set category = craft.categories.id(catId).first() %}

    {% if not category %}
        {% exit 404 %}
    {% endif %}

    {{ category.title }}
{% endif %}


---

