blog_tags function to get only certain tags

SOLVE
New Contributor

Hi there, is there a way to only select certain tags from the call

 

blog_topics

 

I am trying to build a dropdown menu of blog tags.

One with all tags using
{% set my_topics = blog_topics('default', 250) %}

 

and one with only 3 specific tags

Can I select only certain topics with the same function above? Is there a workaround for this?

 

Thanks!

Reply
0 Upvotes
1 Accepted solution

Accepted Solutions
Solution
HubSpot Alumni

Hi @kenwhm,

 

Glad that's working for you. I think if you wanted to, you could simplify your code a bit. You could just write a reverse of the condition and then skip your else statement. In HubL we can get the inverse of an expression with the "not" or "!" operators. So either of these I think would do the trick for you:

 

{% set my_topics = blog_topics('default', 250) %}

{% for my_topic in my_topics %}
  {% if !(my_topic.name == "Digital Transformation" || my_topic.name == "Retail" || my_topic.name == "Fleet Services") %}
     {{my_topic.name}}
  {% endif %}
{% endfor %}

or:

 

{% set my_topics = blog_topics('default', 250) %}

{% for my_topic in my_topics %}
  {% if not (my_topic.name == "Digital Transformation" || my_topic.name == "Retail" || my_topic.name == "Fleet Services") %}
     {{my_topic.name}}
  {% endif %}
{% endfor %}

This way we're looking to see if the topic name is any of "Digital Transformation", or "Retail", or "Fleet Services". Then we're inversing the result. So if the tag is one of those, we'll reverse it so that the condition evaluates to false and that tag doesn't get printed. If the tag isn't one of those selected, we inverse the condition so that it evaluates to true and print the tag.

 

I hope that helps!

 

Leland Scanlan

HubSpot Developer Support

View solution in original post

Reply
0 Upvotes
4 Replies 4
HubSpot Alumni

Hi @kenwhm,

 

I don't think it's possible to plug in specific tags to the blog_tags() function. It just takes 2 arguments: the 1st is the blog ID and the 2nd is the number of tags to pull in. But you could run some conditional logic in your loop when you iterate over the tags. So for example something like this:

 

{% set my_topics = blog_topics('default', 250) %}

{% for my_topic in my_topics %}
  {% if my_topic.name == "Topic one" || my_topic.name == "Topic two" || my_topic.name == "Topic three" %}
    {# HTML goes here #}
  {% endif %}
{% endfor %}

And that way you're only adding the tags into your menu when they're specific tags. Would that work for what you're trying to do?

 

Leland Scanlan

HubSpot Developer Support
Reply
0 Upvotes
New Contributor

hi @lscanlan 

Thanks for your quick response.

 

That worked perfectly.

I just added an else statement to display the ones that do not contain the topics mentioned and it works.

 

 

 {% set my_topics = blog_topics('default', 250) %}

   {% for my_topic in my_topics %}
     {% if my_topic.name == "Digital Transformation" || my_topic.name == "Retail" || my_topic.name == "Fleet Services" %}
        
     {% else %}
        {{my_topic.name}}
     {% endif %}
   {% endfor %}


Is there a diff way to do this within that same code?

 

Reply
0 Upvotes
Solution
HubSpot Alumni

Hi @kenwhm,

 

Glad that's working for you. I think if you wanted to, you could simplify your code a bit. You could just write a reverse of the condition and then skip your else statement. In HubL we can get the inverse of an expression with the "not" or "!" operators. So either of these I think would do the trick for you:

 

{% set my_topics = blog_topics('default', 250) %}

{% for my_topic in my_topics %}
  {% if !(my_topic.name == "Digital Transformation" || my_topic.name == "Retail" || my_topic.name == "Fleet Services") %}
     {{my_topic.name}}
  {% endif %}
{% endfor %}

or:

 

{% set my_topics = blog_topics('default', 250) %}

{% for my_topic in my_topics %}
  {% if not (my_topic.name == "Digital Transformation" || my_topic.name == "Retail" || my_topic.name == "Fleet Services") %}
     {{my_topic.name}}
  {% endif %}
{% endfor %}

This way we're looking to see if the topic name is any of "Digital Transformation", or "Retail", or "Fleet Services". Then we're inversing the result. So if the tag is one of those, we'll reverse it so that the condition evaluates to false and that tag doesn't get printed. If the tag isn't one of those selected, we inverse the condition so that it evaluates to true and print the tag.

 

I hope that helps!

 

Leland Scanlan

HubSpot Developer Support

View solution in original post

Reply
0 Upvotes
Regular Contributor

Hi, @lscanlan - I was wondering if you could take a look at why this isn't working for me when I try to filter out two conditions.

 

This code is working correctly to filter out any of my topics that contain "Minute":

          {% if content.topic_list %}
          <div class="post-header-details topic-listing">
            <span id="post-topic"><i class="fa fa-tags" aria-hidden="true"></i>&nbsp;{% for topic in content.topic_list %}{% if not topic.name is containing "Minute" %}<a class="post-topic-link topic-seq-{{ loop.index }}" href="{{ group.absolute_url }}/topic/{{ topic.slug }}">{{ topic.name }}</a>{% endif %}{% endfor %}</span>
          </div>
          {% endif %}

Now I have another topic that I want filtered out named Personal Training. I tried adding it in using the code below, but it's not filtering it out. It does still filter out the topics containing "Minute" though.

          {% if content.topic_list %}
          <div class="post-header-details topic-listing">
            <span id="post-topic"><i class="fa fa-tags" aria-hidden="true"></i>&nbsp;{% for topic in content.topic_list %}{% if not (topic.name is containing "Minute" || topic.name is containing "Personal") %}<a class="post-topic-link topic-seq-{{ loop.index }}" href="{{ group.absolute_url }}/topic/{{ topic.slug }}">{{ topic.name }}</a>{% endif %}{% endfor %}</span>
          </div>
          {% endif %}

Here's an example post with both topics of "Personal Training" and "15 Minutes": https://go2.o2fitnessclubs.com/library/corrective-exercises-stretches-andrew-032020 (this is for displaying the topics under the title)

 

Thanks in advance!

Reply
0 Upvotes