blog_tags function to get only certain tags

SOLVE
Highlighted
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
HubSpot Moderator

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
Reply
0 Upvotes
3 Replies 3
HubSpot Moderator

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
HubSpot Moderator

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
Reply
0 Upvotes