CMS Development

Guizza
Participant

Topics list item order

Hi all,

In several webpages, we call the blog by ID to show some of the posts content (body, name, topic) but in some case the order of the topics list does not match with what i see in the topics fields of the edit blog panel: i can't reach some of them, although the code is the same.

 

For example:

{% set topic_posts = blog_recent_posts('4493383984', 9) %}
{% set topic_sequence = [4523596089,4661436923,4617624976,4704613263,4523566144,4707609368,4707657173,4619542851] %}
<ul class="cases-items">
{% for topic_post in topic_posts %}
{% if topic_post.id in topic_sequence %}
<li>
<div class="col-dx-img-box">
<a href="{{ topic_post.meta_description }}" target="_blank"><img src="{{topic_post.featured_image}}"></a>
<h4>{{ topic_post.topic_list[1] }}</h4>
<a href="{{ topic_post.meta_description }}" target="_blank"><h3>{{ topic_post.name }}</h3></a>
</div>
</li>
{% endif %}
{% endfor %}
</ul>

 

once i have the post list in the sequence, the topic_post.topic_list[1] gives me different result (the second or the first item indifferently). Can anyone explain me how does it work?

 

Regards.

0 Upvotes
5 Replies 5
Jsum
Key Advisor

Topics list item order

I am not sure if I understand your question completely, so let me know if I have gone off the rails with this.

 

From what I understand, you are receiving the second topic instead of the first?

{{ post.topic_list }} is an array containing all of the topics assigned to that post. If the post has no topics the value is empy ( []) if it has 2 topics the array contains 2 values ( [topic1, topic2]) and etc.

 

arrays in most coding languages start at 0 so:

topic1 = 0

topic2 = 1

topic3 = 2

 

so if your issue is that you are getting the second topic instead of the first then the solutions would be to change {{ topic_post.topic_list[1] }} to {{ topic_post.topic_list[0] }}

 

let me know if that helps.

 

0 Upvotes
Guizza
Participant

Topics list item order

yes, you got it right, @Jsum, but even if i change the index of the array, nothing seems to change.

Again, and if i have more than two items, how do you suggest to solve the issue?

 

This is the real problem.. i don't think that topic_post.topic_list is a standard array...

 

G.

0 Upvotes
Jsum
Key Advisor

Topics list item order

@Guizza, wow that is irratating. I recreated your issue in my test portal to see if I could find a solution and you are right, this array does not act right.

 

The first topic in the array was the 4th that I entered for the post. I attempted to use several different types of filters to see if it would alter the order, or in some cases do anything (http://designers.hubspot.com/docs/hubl/hubl-supported-filters) and most of them didn't do anything. the ones that did were useless in this case, like capitalize. 

 

If you know what topic you are looking for you could do what you did with the sequence only with topics to filter the topics. I really can't think of anything else to do here. They should really fix this to where the orders match. 

 

For the record

 

{% for topic in topic_post.topic_list %} {% if loop.index <= 1 %}{{ topic.name }} {% endif %}{% endfor %}

works but: 

{% for topic in topic_post.topic_list|groupby(name) %} {{ topic.name[0] }}{% endfor %}

and

{% for topic in topic_post.topic_list|groupby(name) %} {{ topic[0].name }}{% endfor %}

 Do not work. The first was a typo, the second is correct syntax for quite a few other languages. not sure why this doesn't work in jinja/python. 

 

Maybe you can find some anwers by search for "ordering array items in jinja". I find a lot of answers to development issues by going straight to the source of Hubspots HubL, jinja. Sorry I couldn't be more help.

Jsum
Key Advisor

Topics list item order

It looks like you are running a for loop for the blog group, but you are not looping through the topics for each blog. have your tried this?

 

 

{% set topic_posts = blog_recent_posts('4493383984', 9) %}
{% set topic_sequence = [4523596089,4661436923,4617624976,4704613263,4523566144,4707609368,4707657173,4619542851] %}
<ul class="cases-items">
{% for topic_post in topic_posts %}
{% if topic_post.id in topic_sequence %}
<li>
<div class="col-dx-img-box">
<a href="{{ topic_post.meta_description }}" target="_blank"><img src="{{topic_post.featured_image}}"></a>
<h4>{% for topic in topic_post.topic_list %}{% if loop.index <= 1 %}{{ topic.name }}{% endif %}{% endfor %}</h4>
<a href="{{ topic_post.meta_description }}" target="_blank"><h3>{{ topic_post.name }}</h3></a>
</div>
</li>
{% endif %}
{% endfor %}
</ul>

I put a loop for the topics inside the blog group loop and limited the output to 1. it should only show the first topic. If you want just the second, third, ect. you would want to use "and" to set up "is greater 1 and less than 3" type of logic, or you can specify an item like you have done before:

{% for topic in topic_post.topic_list %}{{ topic.name[0] }}{% endfor %}

Can you test this and see if it does what you need?

 

0 Upvotes
Guizza
Participant

Topics list item order

thanks for the fast reply, @Jsum.

the point for me is not how can i retrieve the name of the 'n' topic in the topic_list, i'm sure that the code is correct for an array (your last code give me an error in the edit code panel..), but if i try to stamp the {{topic_post.topic_list}} as a comment, the page shows me a list of topics that is NOT in the same order as i write in the blog settings topic field, and it's for that that i reach the wrong name...

G.

0 Upvotes