2

I have a route that matches:

/blog/tagged/{tag}

And in my template I would like to pull up all blogs that are related to the tag:

{% if tag is defined %}
    {% set blogTag = craft.tags({ name: tag, group: 'blogTags' }) %}
    {% set filter = filter|merge({ relatedTo: blogTag }) %}
{% endif %}

{% for entry in craft.entries(filter).find() %}

But this doesn't work since craft.tags() gives me an ElementCriteriaModel, but I need a TagModel to work with relatedTo().

I can't figure out how to get the TagModel here.

2

I figured this one out right after I posted it.

{% set blogTag = craft.tags({ tag: tag, group: 'blogTags' }).first() %}

One mistake was searching for it with "name" instead of "tag".

The other mistake was that I needed to call first() to get the TagModel. Otherwise, craft.tags() just returns an ElementCriteriaModel.

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.