1

I've read through the docs but can't figure out what I'm doing wrong here:

I'd like to display a list of entries by tag, where the tag is in segment 2 of the url.

Here's what I've tried and is not working (they output nothing):

{% set secondSegment = craft.app.request.getSegment(2) %}

{% set diaryEntries = craft.entries.section(['gardenDiary','gardenInfo']).relatedTo({
        sourceSite: craft.app.sites.currentSite.id, 
    element: secondSegment,
    field: 'gardenTags'
}).limit(null).all() %}

Also tried:

{% set diaryEntries = craft.entries.section('gardenDiary').relatedTo(secondSegment).all() %}

...

{% for entry in diaryEntries %}
{{ entry.title }}
...etc...
{% endfor %}
  • Entries are known to be related to tags
  • I've tried adding a known related tag directly - still nothing.
  • If I don't specify a tag, it lists all the entries.
  • Example URL: mydomain.com/tags/fruit
  • Template is loaded via a Route: [Global] tags/* --> tags/index.html

What am I doing wrong? Thanks in advance.

1

The relatedTo param expects an object, an ID or an array of object or ID (docs), in your case secondSegment is a string and this is the issue. Try:

{% set tag = craft.tags.slug( craft.app.request.getSegment(2) ).one() %}
{% set entries = craft.entries.relatedTo(tag).all() %}
  • Thank you! That works. – Adam George Nov 8 at 7:18

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.