Ordering blog_topics() by a custom order (not name or post count)

JordanPak
Member | Gold Partner

I have a blog listing template that lists "groups" of posts by blog topic. For example:

 

  1. Topic 1
    1. Post 1
    2. Post 2
  2. Topic 2
    1. Post 3
    2. Post 4
  3. Topic 3
    1. Post 5

 

Here's a link to a page using the actual template: https://www.neurotour.com/shows. ToursBroadway, and Concerts are a few of the topics, and the "posters" listed under each of them are posts tagged with the topic.

 

Currently, I'm getting them by 

set show_topics = blog_topics( 'shows', 100 )

However, it looks like you can only get blog_topics by name or post count. Does anyone know how to get the topics in a custom order?

 

Thank you for your help

0 Upvotes
10 Replies 10
roisinkirby
HubSpot Product Team

Hey @JordanPak are you still having trouble? If so please let me know and I'll get you connected with a design expert. 

0 Upvotes
ramanverma2005
Participant

yes I am looking for the same 

0 Upvotes
Kevin-C
Recognized Expert | Partner

Hey @JordanPak,

 

The variable show_topics that you set returns a dictionery of values. Since this is a dict you can apply filters to it, allowing you to sort/order/filter/modify it however you like. The map, one of the most powerful filters, combined with any number of other will allow you to create anything you need.

 

What kind of custom order were you looking for?

0 Upvotes
ramanverma2005
Participant

I have the topics in BLOG suppose

 

Apple 

Featured

Health

Daycare

 

I want this should be like 

 

Daycare (2)
Apple (3)

Featured (4)

Health (22)

 

Along with the number of posts

0 Upvotes
Kevin-C
Recognized Expert | Partner

Hey @ramanverma2005  and @JordanPak 

 

Since you know the order you want you shouldn't have to filter and order it dynamically. Saved us a little work!

 

To start, the "topic" keyword has been depreciated, and though it still works(for now) we now should use "tag". See here

 

First define your dictionary:

{% set orderedList = ['Daycare', 'Apple', 'Featured', 'Health'] %}

With the list defined in the order you want, we can loop through the values in the defined order.

 

The loop through the list, get post count and print:

<select id="blogs">
	{% for topic in orderedList %}
  		<option value="topic">{{ topic ~ " (" ~ blog_recent_tag_posts('default', topic, 250 )|count ~ ")" }}</option>
	{% endfor %}
</select>

Here we us the "~" to concatenate the values into the printable string you're looking for.

We use the "blog_recent_tag_post(…,250)" to get 250 of the most recent blogs with that tag as a dict and use the "count" (or length) filter to get the number of posts within that dict.

 

Let me know if this gets you going.

 

 

EDIT:

I'm not 100% if the "blog_recent_tag_post(……)" is case sensitive or not, but if it does cause issue you can use the "lower", "upper", "capitalize", or "title" filters to address the issue.

Note: Remember to use filters to pass modified values rather than modifing the original value.

ramanverma2005
Participant

It works only if the blog is having single topic and showing 0 counter for those are having multiple topics

0 Upvotes
ramanverma2005
Participant

I made some changes and it works thanks for you help @Kevin-C 

Kevin-C
Recognized Expert | Partner

Thats great news!

Would you mind posting the solution that you found so other community members may use it in the future?

0 Upvotes
ramanverma2005
Participant

yes sure will share it today or max tomorrow

ramanverma2005
Participant

{% set orderedList = ['child-care', 'preschool', 'community', 'montessori' , 'home-daycare','activities','cost','homeschool'] %}
<ul>
{% for topic in orderedList %}
<li> <a href="{{group.absolute_url}}/tag/{{topic}}"> {{ topic ~ " (" ~ blog_recent_tag_posts('default', topic|capitalize, 500 )|count ~ ")" }}</a></li>
{% endfor %}
</ul>