Conditionals on blog tags

Top Contributor | Platinum Partner

I would like to create custom layouts for certain tag archive pages. in the listing template, is it possible to write a conditional statement that says 


if post is tagged with {do this} else {do this}


similarly, on the post template, is it possible to do something if it has a particular tag?


3 Replies 3
HubSpot Employee

Hi @mrspabs    Apologies no one has responded yet.   Not sure if you looked though our HUbL docs located here.   Click here to find and explanation of if and unless statements, while this page explains operators and expression tests.


With my limited knowledge of hubl, and looking through the docs above, I'm not seeing that it is possible. Tagging a few design partners for further help. @jonlcrow  @Jlamb1@JasonRosa  Can you assist? 


Thank you,

Ed Justen

Did my post help answer your query? Help the Community by marking it as a solution

In theory it should be very doable. It seems like first do a for loop to go through each post then an if statement for tag="My Topic". Something like:


{% set posts = blog_recent_posts('default', 4) %}
{% for post in posts %} <!-- loop through posts in custom all posts variable -->
{% if  post.topic =="My Topic" %}

<div class="post-item">
<p class="post-listing-simple"><a href="{{post.absolute_url}}">{{ }}</a></p>

{% else %} <p>nbsp&;</p>

{% endfor %}


More on if statements


One thing I am not sure of is whether you may need to iterate through the possible list of topics. But play with it and see what you get.

HubSpot Employee

@jonlcrow yup your thought process is right on point here. Here is some code from a sample where I did something like this in the blog listing before: 


{% for content in contents %}
{% for topic in content.topic_list %}
{% if == "Images" %}
  <!-- code for posts with topic name of Images -->
{% else %}
  <!-- default code without topic --> 
{% endif %}
{% endfor %}
{% endfor %}


For the post HTML it would be the same idea except you wouldn't need the outer for content in contents loop.