3

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

  1. I first created a category group ('categories')
  2. I added several categories to the group ('innovation', 'teams', etc)
  3. I added a category field to my blog post matrix field (post.category)
  4. 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 %}
     <li><a href="{{ category.url }}">{{ category.title }}</a></li>
     {% endfor %}
    
  5. 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)

  6. In the template for the category I can list the title and name but I want to get the entries so I:

    <h1>{{ category.title }}</h1> <--- just this works
    {% set posts = craft.entries.relatedTo(category) %}
    {% for post in posts %}
    <p>{{ post.title }}</p>
    {% endfor %}
    

But this didn't work

following http://buildwithcraft.com/help/entries-related-to-category

  1. then I tried this, thinking specifying the field was necessary

    <h1>{{ category.title }}</h1> <--- again, this works
    {% set posts = craft.entries({
    section: 'blog',
    relatedTo: {
    targetElement: category,
    field: 'posts.categories'
    }
    }) %}
    
    {% for post in posts %}
    <p>{{ post.title }}</p>
    {% endfor %}
    

and this doesn't work either.

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

  • Do the relations work on your blog's index page? Try adding {{ craft.entries.relatedTo(category).first().title }} to the categories loop. – carlcs Mar 19 '15 at 2:09
  • Could you enable devMode and tell us what happens if you {{ dump(posts) }}? – Paul Mar 19 '15 at 7:33
  • 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 Mar 19 '15 at 15:13
  • Great :) Can you create an answer this and mark it as correct? – darylknight Jun 25 '15 at 14:31
6

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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