3

I'm trying to create a category driven gallery. My setup (right or wrong) is this:-

Users create a new entry within a Structure, title it, add photos and then assign the entry to a pre-defined gallery category.

My idea is that when a visitor lands on the 'Gallery' page they see a 'grid' of latest photos (this is a question which might appear here later), with a navigation on the side which allows them to browse the other galleries (categories).

So, how do I list out the categories and associated entries for this navigation?

Example:

  • Category Title One
    • Title of the entry1
    • Title of the entry2
    • Title of the entry3
  • Category Title Two
    • Title of the entry4
    • Title of the entry5
  • Category Title Three
    • Title of the entry6
    • Title of the entry7
    • Title of the entry8
  • Category Title Four
    • Title of the entry9

I don't want the Category Titles to be links, just the entries within them.

I hope this makes sense and someone can provide a solution.

Many thanks

Martin

1 Answer 1

3

Something along the line of this should do that:

<ul>
    {% for category in craft.categories.group('categoryGroupHandle') %}
    <li>
        {{ category.title }}
        {% for entry in craft.entries.section('sectionHandle').relatedTo(category) %}
            {% if loop.first %}<ul>{% endif %}
                <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
            {% if loop.last %}</ul>{% endif %}
        {% endfor %}
        </ul>
    </li>
    {% endfor %}
</ul>

Probably some bugs in here, but should be enough to get you started.

Also probably smart to wrap this in {% cache %} tags, as it probably will trigger a lot of database queries.

1
  • Brilliant! Many thanks for this. I also managed to get it working with a definition list which felt more semantic for my purposes.
    – Martin
    Aug 1, 2015 at 8:11

Your Answer

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

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