2

On my page of all tags, it works great except for tags that have more than one word. The URL is replacing the spaces with %20 as one would expect.

http://www.colorcomputergames.com/tags/

I tried using:

{{ siteUrl }}tags/{{ tag.title | kebab }}

This works great in changing the %20 to hyphens. However, if I click on a tag with multiple words I go to page not found. Makes sense as the system doesn't see that tag.

  {% extends "_layout" %}
    {% set title = "Tags" %}
    {% block content %}
    {% for tag in craft.tags.order('title') %}
        <li><a href="{{ siteUrl }}tags/{{ tag.title | kebab }}">{{ tag.title }}</a></li>
    {% endfor %}
    {% endblock %}

So next I tried:

{% extends "_layout" %}
{% set title = "Tags" %}
{% block content %}
{% for tag in craft.tags.order('title') %}
    <li><a href="{{ siteUrl }}tags/{{ tag.title | url_encode }}">{{ tag.title }}</a></li>
{% endfor %}
{% endblock %}

But that wasn't ideal either.

What is the best way to deal with this issue?

1 Answer 1

3

Update.

After looking at the tag model a little more closely, it looks like tags have both a title and slug — so if you use the slug to construct your url then you shouldn't have that problem.

For your tag index:

<ul>
    {% for tag in craft.tags.order('title') %}
        <li><a href="{{ siteUrl }}tags/{{ tag.slug }}">{{ tag.title }}</a></li>
    {% endfor %}
</ul>

In your tag index page (assuming you are using the tag token in your custom route):

{% set tagEntry = craft.tags.group('myTagGroup').slug(tag).first %}
{% set entries = craft.entries.relatedTo(tagEntry).order('title').limit(null) %}
2
  • I modified my original code based on the answer here (craftcms.stackexchange.com/questions/881/…) I then tried your code with the newer code. I have the following, and while it replaces the spaces with hyphens, I still get 404 errors with tags containing two words.
    – DevServe
    Commented Dec 29, 2016 at 2:57
  • My mistake. First, it looks like tags have a slug that is kebab case by default. And secondly, I think that you will need a tag model to use in relatedTo(tag). I updated the answer... see if that works better. Commented Dec 29, 2016 at 4:46

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.