CMS Development

Theodor
Participante | Partner nivel Elite
Participante | Partner nivel Elite

Related Post Loop

Hello Hubspot Devs !!!!

It seems that my problems always revolve around blog loops. So I have this related blog loop : 

{% if content.topic_list %}
    {% for topic in content.topic_list %}
        {% if loop.first %}
            {% set related_posts = blog_recent_topic_posts(module.blog_id, topic.slug,  2) %}
            {% for post in related_posts %}
                {% unless content.absolute_url == post.absolute_url %}
<div>My Markup like : 
<h3><a href="{{ post.absolute_url }}">{{ post.name }}</a></h3>
</div>
{% endunless %}
            {% endfor %}
        {% endif %}
    {% endfor %}
{% endif %}

The idea is to get only two suggestions for the current article.
I use the "unless" loop to remove the current article from the suggestions.
With this setup I only get 1 or none article suggestion. If I increase the suggestion count to 3, I get two suggestions, but sometimes 3 (which is not the goal).
I tried to get more articles something like : 

{% set related_posts = blog_recent_topic_posts(module.blog_id, topic.slug,  10) %}
            {% for post in related_posts %}
{% if loop.index <= 2 %}
Again my markup
{% endif %}
{% endfor %}

It doesn't work at all. I suspect I only get one article because of the "unless" condition. The thing is that if I remove the "unless" I get the current article in suggestion wich defies the purpose of a related articles block. 

Any help will be kindly appreciated. 

 

Kind regards,

 

Theodor

0 Me gusta
2 Respuestas 2
lscanlan
Exmiembro de HubSpot
Exmiembro de HubSpot

Related Post Loop

Hi @Theodor,

 

I think that logic looks correct. When you're only getting 0 or 1 posts (when you're limiting related_posts to 2), do you know for sure that more posts should be pulling through? Is it possible that the current post is also being pulled in to the related_posts dict? If it's one of the most recently published posts for that topic, I'd expect it to be one of the posts in that dict.

 

You could also try printing out that dictionary to see what the function is actually pulling in. So for example somewhere in your HTML:

 

<div id="testing" style="display: none;">
  {{ related_posts|pprint }}
</div>

 

Beyond that, I'm happy to look at any specific pages if you want to link them here.

 

Leland Scanlan

HubSpot Developer Support
Theodor
Participante | Partner nivel Elite
Participante | Partner nivel Elite

Related Post Loop

Hi @lscanlan ,

 

Sorry for my late answer. 

 

I got it to work. 

 

Thank you very much,

 

kind regards,

 

Theodor

0 Me gusta