2

I'm developing a masonry photo gallery with some filters (colors, style, manufacturer, etc) and I want to use tags to filter them by. I need some navigation which should look like this:

<ul>
    <li>Tag Group 1
        <ul>
            <li>Tag 1</li>
            <li>Tag 2</li>
        </ul>
    </li>
    <li>Tag Group 2
        <ul>
            <li>Tag 3</li>
            <li>tag 4</li>
        </ul>
    </li>
</ul>

I read this article https://craftcms.com/support/active-tags but I can't figure out how to get all group tags and for them all the tags that are actualy in use. I think i should use

{% for tag in craft.tags.group('blogTags') %}

but I want to get all the group handles in a loop dynamically.

1 Answer 1

4

You could use the group filter to group your tags by tag groups:

{% set entries = craft.entries.section('gallery').limit(null) %}

<ul>

    {# Get all the tags related to our gallery entries and group them by tag group #}
    {% set allGalleryTags = craft.tags.relatedTo(entries)|group('group') %}

    {# Loop the tag groups #}
    {% for tagGroup, tagsInGroup in allGalleryTags %}

        <li>
            <h2>{{ tagGroup }}</h2>
            <ul>

                {# Loop the tags of this group #}
                {% for tag in tagsInGroup %}
                    <li>{{ tag.name }}</li>
                {% endfor %}

            </ul>
        </li>

    {% endfor %}

</ul>
2
  • Awesome. Thank you very much dude. It works flawless.
    – Cipick
    Nov 19, 2014 at 8:38
  • Glad to help, @Cipick. If you have lots of tags you could further group them by the first letter: {% for letter, tagByLetter in tagsInGroup|group('name|slice(0, 1)') %}.
    – carlcs
    Nov 19, 2014 at 9:19

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.