CMS Development

MIND
Participant

Only show the first topic

I have a blog that shows post topics overlayed on the featured image. I want to be able to add more topics, but only want to show the first topic.

 

Here is the code I'm working with. What's the best way to do this?

 

{% set rec_posts = blog_recent_posts('4110896620', 1) %}
{% if is_listing_view %}
{% for rec_post in contents|first %}
 
 <div class="post-item">
	<div class="hs-featured-image-wrapper set-bg">	
        <div class="get-bg">
               <a href="{{ rec_post.url }}">
                 {% if rec_post.featured_image %}
                    <img src="{{ rec_post.featured_image }}" alt="feature image">
                 {% else %}
                    <img src="http://cdn2.hubspot.net/hubfs/237516/Graphics/JiJi_graphics/Blog_Default.jpg" alt="feature image">
                 {% endif %}
               </a>
        </div>
            {% if rec_post.topic_list %}
                <p id="hubspot-topic_data">
                {% for topic in rec_post.topic_list %}
                    <a class="topic-link {{ topic.slug }}" href="{{ group.absolute_url }}/topic/{{ topic.slug }}">{{ topic.name }}</a>{% if not loop.last %},{% endif %}
                {% endfor %}
                </p>
            {% endif %}
         </div>
	<div class="post-header">
    	<h2><a href="{{ rec_post.url }}">{{ rec_post.name }}</a></h2>
        <p> {{ rec_post.meta_description  }} </p>
        <p id="hubspot-author_data"> By <a class="author-link" href="{{ group.absolute_url }}/author/{{ rec_post.blog_post_author.slug }}" > {{ rec_post.blog_author.display_name }} </a> </p>
    </div>
</div>
{% endfor %}
{% endif %}
0 Upvotes
4 Replies 4
Jsum
Key Advisor

Only show the first topic

@MIND,

 

if you limit your topics loop:

{% if rec_post.topic_list %}
                <p id="hubspot-topic_data">
                {% for topic in rec_post.topic_list %}
                     {% if loop.index <= 1 %}
                          <a class="topic-link {{ topic.slug }}" href="{{ group.absolute_url }}/topic/{{ topic.slug }}">{{ topic.name }}</a>
                     {% endif %}
                {% endfor %}
                </p>
            {% endif %}
MIND
Participant

Only show the first topic

Thanks @Jsum!

I actually figured that out late last night after posting. It also seems that adding "|first" at the end of ".topic_list" has the same result.

My problem now is that Hubspot doesn't recognize which Topic is ADDED to the post first. It goes by some other metric. I've had Topics I added to the post 2nd or 3rd show up as the first.

Jsum
Key Advisor

Only show the first topic

@MIND,

 

Instead of relying on the actual topics list you could add a choice or text input field to your posts, export this to the template, and pull it into the listing page. This way you don't have to worry about sorting the topics array.

ksokolov
Participant

Only show the first topic

Can you please show an example of how to code this?

0 Upvotes