CMS Development

BarryGrennan
Top Contributor

Get first tag in tag list, unless it's featured, then get second

SOLVE

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 %}
1 Accepted solution
BarryGrennan
Solution
Top Contributor

Get first tag in tag list, unless it's featured, then get second

SOLVE

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>
 

View solution in original post

2 Replies 2
BarryGrennan
Solution
Top Contributor

Get first tag in tag list, unless it's featured, then get second

SOLVE

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>
 
Ntbrown
Contributor

Get first tag in tag list, unless it's featured, then get second

SOLVE

content.tag_list|rejectattr("slug", "equalto", "featured")|first

 

What about the null case? This is also heavy handed filtering everything, but cleaner.

0 Upvotes