2

I'm new to all of this and so far Categories from a templating point of view are confusing me a bit. I'm loving Craft so far but some concepts are causing me some headaches to adjust to!

What i'm trying to do is list the sub-categories a blog post is part of.

I have 6 categories and each has 4 sub-categories. I only want to list the sub-categories.

So when someone is viewing the site and they click into a full blog post page, at the top i'm listing the title, author, posted date and i want to follow that with a lisrt of the sub-categories applied to that blog post.

5

This will list all the categories that are assigned to the entry regardless of level.

<ul>
    {% for category in entry.categoriesFieldHandle %}
        <li><a href="{{ category.url }}">category.title</a></li>
    {% endfor %}
</ul>

If you only want to list the subcategories, (regardless of whether a main category is also assigned) then you can add a test for the category level.

<ul>
    {% for category in entry.categoriesFieldHandle %}
        {% if category.level > 1 %}
            <li><a href="{{ category.url }}">category.title</a></li>
        {% endif %}
    {% endfor %}
</ul>
  • I would actually include the level argument in the for loop. That field is just an element criteria model until you actually run it. Something like this: {% for category in entry.categoriesFieldHandle.level(1) %} – Bryan Redeagle Jan 23 '15 at 12:36
  • Beautiful! Thanks to both of you :^) I combined your methods (changing level(1) to level('> 1') to get sub-categories in the loop) and it works a charm! – Dan Owen Jan 23 '15 at 15:25
  • Great! That works too. I wasn't sure if you maybe wanted to something slightly different with each level. – Douglas McDonald Jan 23 '15 at 17:09

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.