2

I'm trying to create a condensed overview of my website content on my homepage. I'd like that page to loop through all sections, in each section looping through the categories that apply to the section, and for each category to pull the title, uri, and excerpt of the newest three entries.

I'm having trouble wrapping my head around how I'd do that.

What I'm after might be seen as:

SECTION
  CATEGORY 1 [title, description, view category archive]
    POST 1 [title, excerpt, publication date, view entry]
    POST 2 [title, excerpt, publication date, view entry]
    POST 3 [title, excerpt, publication date, view entry]
  CATEGORY 2 [title, description, view category archive]
    POST 1 [title, excerpt, publication date, view entry]
    POST 2 [title, excerpt, publication date, view entry]
    POST 3 [title, excerpt, publication date, view entry]
SECTION 2
  CATEGORY 1 [title, description, view category archive]
    POST 1 [title, excerpt, publication date, view entry]
    POST 2 [title, excerpt, publication date, view entry]
    POST 3 [title, excerpt, publication date, view entry]

For the sake of simplicity, all my categories are currently under a 'global' group. They also map pretty much 1:1 to the EntryTypes available in each section. (I'm not entirely convinced I'm using Categories / EntryTypes correctly, given that 1:1 relation... in fact that's another question to ask).

2
  • Am I correct that you only want channel sections? Commented Jun 26, 2014 at 15:21
  • Yes that's right; no point looping through Singles and I don't have any Structures yet. Commented Jun 26, 2014 at 15:28

1 Answer 1

4

You need to loop over the sections. In each section, you then loop over the categories, and display your entries inside them. Something like this.

{% set categories = craft.categories.group('groupHandle').find() %}
{% for section in craft.sections.getAllSections() %}
    {% if section.type == 'channel' %}
        <h1>{{ section.name }}</h1>
        {% for category in categories %}
            <h2>{{ category.title }}</h2>
            {% for entry in craft.entries.section(section).relatedTo(category).limit(3).find() %}
                <a href="{{ entry.url }}">{{ entry.title }}</a>
            {% endfor %}
        {% endfor %}
    {% endif %}
{% endfor %}

This is a pretty quick and dirty example. I pull the categories ahead of time so I can reduce the number of database calls. I suggest using the cache to speed things up.

The template docs (especially the ElementCriteriaModel) are handy reading materials.

1
  • Awesome, thanks Bryan! I'm sure the docs will make more sense once I've used Craft a bit more - they seem to make sense but then I struggle to 'join the dots' as it were. Concrete examples like you just gave help me a bit more than the abstracted docs I think - at least until I wrap my head around what's going on more :) Thanks again! Commented Jun 26, 2014 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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