1

I've followed this guide for creating a routes to list entries with specific tags. It is working fine except that I have muli-word tags so using the tag token from Settings -> Routes gives me urls like

mysite.com/tags/my%20tag%20name

I'd like to us the tag.slug instead which should allow me to have urls end in .../my-tag-name instead, but I can't figure out how to do it.

Is there a way to refer to tag.slug in craft/config/routes.php?

If so what should the custom route look like:

'tags/what goes here?' => 'tags/_tag.html',

Updated

The template code is:

{% extends "_layout" %}


{% block content %}

{% set tag = craft.tags.title(tag).first() %}

{% if not tag %}
    {% exit 404 %}
{% endif %}

<h2>Entries tagged with “{{ tag.title }}”</h2>

{% set entries = craft.entries.relatedTo(tag).order('title') %}

{% if entries | length %}

        {% for entry in entries %}
            {{ include('./partials/entryListItem.html', {'entry': entry}) }}
        {% endfor %}
{% else %}
    <p>No entries could be found with that tag.</p>
{% endif %}

{% endblock %}

and the include points to:

<div class='entry-list-item'>
        <h2><a href="{{ entry.url }}">{{ entry.title }}</a></h2>
        <p>
          <a href="{{ entry.url }}">
            <span class="sm-txt"> continue reading</span>
          </a>
        </p>
        {% if entry.tags | length %}
          <p class='sm-txt'>
          {% for tag in entry.tags %}
            <a href="{{ siteUrl }}tags/{{ tag.title | url_encode }}">{{ tag.title }}, </a>
          {% endfor %}
          </p>
        {% endif %}
      </div>

The route is set in the dashboard tags/(tag token) points to tags/_tag.html

If I change the route to tags/(slug token) I get template error: variable tag does not exist.

  • Can you provide the code you are using in your template and also what your route looks like? – Fyrebase Sep 28 '16 at 14:11
3

You do not have to change your route. Instead modify your template where you are linking the tag.

Instead of using

<a href="{{ siteUrl }}tags/{{ tag.title | url_encode }}">{{ tag.title }}, </a>

Use

<a href="{{ siteUrl }}tags/{{ tag.slug }}">{{ tag.title }}, </a>

UPDATED

To clarify the above snippet goes in the template that generates the link to take the the user to your _tag.html template, not in the _tag.html template itself.

Your route should be as below:

enter image description here

UPDATE 2

Above snippet updated to match your example above.

UPDATE 3

As the tag variable made available to your template is now the tag’s slug, make sure to update the code to query for the tag model to use the slug param.

{% set tag = craft.tags.slug(tag).first() %}
  • I get a 404 error when I do that – Andrew Staroscik Sep 28 '16 at 14:10
  • The above snippet goes in the template that creates the link to your tag not in your _tag.html template – Fyrebase Sep 28 '16 at 14:22
  • I've added more code to the question. Sorry for the lack of clarity. For the route, I am not typing (tag token) I am using the token option provided by the edit route dialogue box. If I type /tags/tag_token in the URI I get 404 – Andrew Staroscik Sep 28 '16 at 14:41
  • tag_token is not meant to be typed, it is a representation of the token option you are using. I added an image instead to clarify. – Fyrebase Sep 28 '16 at 14:48
  • 1
    @AndrewStaroscik I just updated the answer to add the changes to the criteria model we talked about in Craft Slack. – carlcs Sep 29 '16 at 7:30

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.