Mar 24, 2021 5:54 PM
Can't quite figure out the logic I need here
I want to print only one tag on the blog list items. If the first tag is "featured" I want it to print the second instead.
Here's my code at the moment:
{% for tag in content.tag_list %}
{% if loop.index == 1 %}
{% unless tag.slug == 'featured' %}
<div class="tag-icon {{tag.slug}}-icon">{{tag.slug}}</div>
{% endunless %}
{% endif %}
{% endfor %}
Solved! Go to Solution.
Mar 24, 2021 6:13 PM
I figured out a solution,
I created an array of the tag slugs excluding the slug "featured", then I called the first value form the array.
{% set tag_array = [] %}
{% for tag in content.tag_list %}
{% unless tag.slug == 'featured' %}
{% do tag_array.append( tag.slug ) %}
{% endunless %}
{% endfor %}
<div class="tag-icon {{ tag_array[0] }}-icon">{{ tag_array[0] }}</div>
Mar 24, 2021 6:13 PM
I figured out a solution,
I created an array of the tag slugs excluding the slug "featured", then I called the first value form the array.
{% set tag_array = [] %}
{% for tag in content.tag_list %}
{% unless tag.slug == 'featured' %}
{% do tag_array.append( tag.slug ) %}
{% endunless %}
{% endfor %}
<div class="tag-icon {{ tag_array[0] }}-icon">{{ tag_array[0] }}</div>
Mar 24, 2021 6:09 PM - edited Mar 24, 2021 6:10 PM
content.tag_list|rejectattr("slug", "equalto", "featured")|first
What about the null case? This is also heavy handed filtering everything, but cleaner.