2

I understand how to set up a route and template that allows me to display entries by tag.

But how can I set up a template + route to grab multiple tags (if supplied but could just be one) from the url and display entires that have these tags?

I would also like to add a category element (showing all the entires from a category with the selected tags) but suspect much of that may be answered by the first part.

For example

Have a form the builds a url depending on user selections

mysite/queryurl&tags=cheese,egg,bacon&cat=sandwich

Is this possible?

1
  • I have just discovered {% set query = craft.request.getParam('tags') %} on the search example page. I will investigate this.
    – jamus
    Commented Aug 13, 2014 at 16:03

2 Answers 2

4

As long as you're using commas to separate the tag names in the URL:

mysite.com?queryurl&tags=cheese,egg,bacon&cat=sandwich

then you should be able to retrieve the multiple tags like so:

{% set tagsString = craft.request.getParam('tags') %}

{% set tags = craft.tags.name(tagsString).find() %}

<ul>
    {% for tag in tags %}
        <li>{{ tag.name }}</li>
   {% endfor %}
{/ul>
2
  • So I shouldn't be creating a route at all - I should be using the search instead? Small typo on the closing </ul> element.
    – jamus
    Commented Aug 14, 2014 at 10:13
  • This code seems to work without the code above being required to set "tags"? <ul> {% for tag in tags %} <li>{{ tag.name }}</li> {% endfor %} {/ul>
    – jamus
    Commented Aug 14, 2014 at 13:49
1

Ok. I think I worked this out (with a little help from Brad and the search example page). Though there might be a better way of achieving the same result.

    <ul>Looking for:
    {% for tag in tags %}
        <li>{{ tag.name }}</li>
    {% endfor %}
    </ul>

The code above demonstrates that Craft pulls the tags from the URL into the an array called "tags" automatically(?). For each entry I loop through these tags - for each one loop through the entries tags to see if they match. I set matchFound to false before starting and at the end of each entry. If I hit a matching tag I set it to true to remove entries being listed multiple times.

{% for tag in tags %}

        {% for thisItemTag in entry.tags %}

                {% set lookingFor = tag.name %}
                {% set currentTag = thisItemTag %}

                <!-- <li>{{ thisItemTag }}</li> -->
                {% if lookingFor == currentTag %}
                    {% if matchFound == false %}
                        {% set matchFound = true %}
                        <article class="product">
                            <h3>{{ entry.title }}</h3>
                        </article>
                    {% endif %}
                {% endif %}
        {% endfor %}

    {% endfor %}
    {% set matchFound = false %}

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.