0

I want my <ul> to show a single tag representing all "active" entries ( "active" using a custom lightswitch field in the entry: fellowStatus.

Each tag is a year (e.g. 2014, 2010, etc)

E.g. if there are no "active" entries with the tag 2017, it should not display.

The code block below spits out only tags where entries are "active" BUT... It spits out duplicates of each tag depending on how many entries are using it. (e.g. It displays 8 instances of 2015 instead of 1)

<ul>
{% for entry in craft.entries.section('fellows').limit(null) %}

  {% set tags = craft.tags.relatedTo(entry).limit(1).find() %}
  {% set active = entry.fellowStatus %}

    {% if entry.fellowStatus == 0 %}

      {% for tag in tags %}
          <li>
            <a href="{{ tag }}">{{ tag.title }}</a>
          </li>
      {% endfor %}

    {% endif %}

{% endfor %}
</ul>
2
  • Do you want to show all tags related to your entries or do you want to display all tags for each entry? Commented Apr 27, 2018 at 4:36
  • I want to show all tags that are in use. E.g. If 2015 has an entry, show. If 2001 doesn't have an entry, don't show. If 2017 has an entry, but fellowStatus light switch is off, don't show. Commented Apr 30, 2018 at 18:15

1 Answer 1

1

First: grab all entries that match your criteria

Second: grab all Tags related to your entries

PHP

$entryIds = Entry::find()
    ->yourTagField(':notempty:')
    ->fellowStatus('1')
    ->section('fellows')
    ->ids();
$tagsRelatedToEntries = Tag::find()
    ->relatedTo($entryIds)
    ->all();

Twig

{% set entryIds = craft.entries
    .section('fellows')
    .fellowStatus('1')
    .yourTagField(':notempty:')
    .ids() 
%}
{% set tags = craft.tags.relatedTo(entryIds).all() %}

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.