CMS Development

mrspabs
Top Contributor | Elite Partner
Top Contributor | Elite Partner

Conditionals on blog tags

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?

 

0 Upvotes
3 Replies 3
jonlcrow
Member

Conditionals on blog tags

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}}">{{ post.name }}</a></p>

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

</div>
{% endfor %}

 

More on if statements

https://designers.hubspot.com/docs/hubl/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.

JasonRosa
HubSpot Employee
HubSpot Employee

Conditionals on blog tags

@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 topic.name == "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. 

edjusten
HubSpot Employee
HubSpot Employee

Conditionals on blog tags

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