Know if a post has several topics

Hi Developers,

I tell you my problem
I have a blog with different categories (tag / topics) and on the sidebar of the post template I need to put a different image according to the category to which they belong.
So far no problem, with the “for topic” and the “if”, “elif” and “else” the image I want is shown, the problem comes when a post has more than one assigned category that shows several images and only I want one.

This is the code that I have made:

{% for topic in content.topic_list %}
{% if 'transporte' in topic.slug %}
 <a href="link1" rel="noreferrer" target="_blank" >
 <img class="hs-cta-img lazy" src="image_cta01.png" alt="{{ topic|title }}"/>
 </a>
{% elif 'logistica' in topic.slug %}
 <a href="link2" rel="noreferrer" target="_blank" >
 <img class="hs-cta-img lazy" src="image_cta02.png" alt="{{ topic|title }}"/>
 </a>
{% else %}
 <a href="link3" rel="noreferrer" target="_blank" >
 <img class="hs-cta-img lazy" src="image_cta03.png" alt="{{ topic|title }}"/>
 </a>
{% endif %}
{% endfor %}

Thank you very much for the help!

Hey @Nuria-IC,

I believe what you can do here is to first get the count of the total topics and you can do so like this:

 {% if content.topic_list %}
 {% set topic_index_count = [] %}
 <p id="hubspot-topic_data" > Topics:

 {% for topic in content.topic_list %}
 {% if topic_index_count.append(1) %}
 <a class="topic-link" href="{{ group.absolute_url }}/topic/{{ topic.slug }}">{{ topic.name }}</a>{% if not loop.last %},{% endif %}
 {% endif %} 
 {% endfor %}
 <p> Topic Count: {{topic_index_count|length }} </p>
 <p>
 {% endif %}

In this case, you can go ahead and modify your code to place another if/else loop after the for loop to check if topic_index_count|length > 1 then display one image.

Hope this helps and do let me know if there’s any further question on this!