<p>I should note that the <code>relatedTo</code> method accepts a <code>CategoryModel</code>, <em>not</em> the title of a category. What you ended up doing works, but here's the way to do with the <code>relatedTo</code> method, which is what I'd suggest for anyone finding this in the future:</p>

<p><code>{% for entry in craft.entries.relatedTo(craft.categories.slug('your-cat-slug')) %}</code></p>

<p>You now have all entries that are associated with the given category, and can loop through them with the usual <code>{% for entry in entries %}</code> technique.</p>

<p><strong>TL;DR</strong>: You can't pass the category name. You <em>have</em> to give <code>relatedTo</code> an actual <code>CategoryModel</code>.</p>

CC BY-SA 3.0
2015-10-15T00:30:18.813Z
2015-10-15T00:30:18.813Z
Taylor Daughtry
<p>So i'm trying to output blog entries related to a category.</p>

<ol>
<li>I first created a category group ('categories')</li>
<li>I added several categories to the group ('innovation', 'teams', etc)</li>
<li>I added a category field to my blog post matrix field (post.category)</li>
<li><p>On my blog index page (example.com/blog) I have excerpts of posts and a sidebar which has the categories and their urls</p>

<pre><code> {% set categories = craft.categories.group('categories') %}

 {% for category in categories %}
 &lt;li&gt;&lt;a href="{{ category.url }}"&gt;{{ category.title }}&lt;/a&gt;&lt;/li&gt;
 {% endfor %}
</code></pre></li>
<li><p>When I click on the link it goes to the page I expect (example.com/categories/teams) and is using the template i specified for the template group (categories.html)</p></li>
<li><p>In the template for the category I can list the title and name but I want to get the entries so I:</p>

<pre><code>&lt;h1&gt;{{ category.title }}&lt;/h1&gt; &lt;--- just this works
{% set posts = craft.entries.relatedTo(category) %}
{% for post in posts %}
&lt;p&gt;{{ post.title }}&lt;/p&gt;
{% endfor %}
</code></pre></li>
</ol>

<p>But this didn't work</p>

<p>following <a href="http://buildwithcraft.com/help/entries-related-to-category" rel="nofollow">http://buildwithcraft.com/help/entries-related-to-category</a></p>

<ol start="7">
<li><p>then I tried this, thinking specifying the field was necessary</p>

<pre><code>&lt;h1&gt;{{ category.title }}&lt;/h1&gt; &lt;--- again, this works
{% set posts = craft.entries({
section: 'blog',
relatedTo: {
targetElement: category,
field: 'posts.categories'
}
}) %}

{% for post in posts %}
&lt;p&gt;{{ post.title }}&lt;/p&gt;
{% endfor %}
</code></pre></li>
</ol>

<p>and this doesn't work either. </p>

<p>Anyways, i'm not sure where I am going wrong and any help would be appreciated.</p>

CC BY-SA 3.0
2015-03-19T02:09:31.513Z
Do the relations work on your blog's index page? Try adding `{{ craft.entries.relatedTo(category).first().title }}` to the categories loop.
carlcs
CC BY-SA 3.0
2015-03-19T07:33:57.880Z
Could you enable devMode and tell us what happens if you `{{ dump(posts) }}`?
Paul
CC BY-SA 3.0
2015-03-19T15:13:00.683Z
So I realized it was the way I was referencing the categories field nested in the matrix in my templates. I tried several different combinations but no luck. Anyways, I then created a categories field (called categories) and added it to my blog entry type. Then in my individual category template (example.com/categories/innovation) I was able to reference the entries this way                                                                                                           ` {% for entry in craft.entries.section('blog').relatedTo(category) %} `
tahr
CC BY-SA 3.0
2015-06-25T14:31:21.283Z
Great :) Can you create an answer this and mark it as correct?
darylknight
<entries><categories>
Craft entries related on a category page
# Craft entries related on a category page

(Asked by: username_0 on 2015-03-18)

So i'm trying to output blog entries related to a category.

I first created a category group ('categories')
I added several categories to the group ('innovation', 'teams', etc)
I added a category field to my blog post matrix field (post.category)
On my blog index page (example.com/blog) I have excerpts of posts and a sidebar which has the categories and their urls
 {% set categories = craft.categories.group('categories') %}

 {% for category in categories %}
 {{ category.title }}
 {% endfor %}

When I click on the link it goes to the page I expect (example.com/categories/teams) and is using the template i specified for the template group (categories.html)
In the template for the category I can list the title and name but I want to get the entries so I:
{{ category.title }} <--- just this works
{% set posts = craft.entries.relatedTo(category) %}
{% for post in posts %}
{{ post.title }}
{% endfor %}


But this didn't work
following http://buildwithcraft.com/help/entries-related-to-category

then I tried this, thinking specifying the field was necessary
{{ category.title }} <--- again, this works
{% set posts = craft.entries({
section: 'blog',
relatedTo: {
targetElement: category,
field: 'posts.categories'
}
}) %}

{% for post in posts %}
{{ post.title }}
{% endfor %}


and this doesn't work either. 
Anyways, i'm not sure where I am going wrong and any help would be appreciated.

**Tags:** entries, categories

> **Comments:**
> **username_1**: Do the relations work on your blog's index page? Try adding `{{ craft.entries.relatedTo(category).first().title }}` to the categories loop.  
> **username_2**: Could you enable devMode and tell us what happens if you `{{ dump(posts) }}`?  
> **username_0**: So I realized it was the way I was referencing the categories field nested in the matrix in my templates. I tried several different combinations but no luck. Anyways, I then created a categories field (called categories) and added it to my blog entry type. Then in my individual category template (example.com/categories/innovation) I was able to reference the entries this way                                                                                                           ` {% for entry in craft.entries.section('blog').relatedTo(category) %} `  
> **username_3**: Great :) Can you create an answer this and mark it as correct?  

---

## 1 answers

### Answer by username_4 on 2015-10-15. Score: 8 | Accepted answer

I should note that the `relatedTo` method accepts a `CategoryModel`, *not* the title of a category. What you ended up doing works, but here's the way to do with the `relatedTo` method, which is what I'd suggest for anyone finding this in the future:

`{% for entry in craft.entries.relatedTo(craft.categories.slug('your-cat-slug')) %}`

You now have all entries that are associated with the given category, and can loop through them with the usual `{% for entry in entries %}` technique.

**TL;DR**: You can't pass the category name. You *have* to give `relatedTo` an actual `CategoryModel`.


---

