If statement based on subdomain

Highlighted
Occasional Contributor

I'm trying to create a recent posts section using this code. This will display at the bottom of blog posts. I have 4 blogs in Hubspot on different subdomains. Is it possible to write an if statement that will print the recent posts based on which blog it's on? If that isn't possible, is there some other way of doing this without creating 4 separate blog post templates?

 

I was thinking something like:

{% if blogid='subdomain1' %}
{% set posts = blog_recent_posts('SUBDOMAIN1', 3) %}
{% for post in posts %}
    <h3><a href="{{ post.absolute_url }}">{{ post.name }}</a></h3>
    <img src="{{ post.featured_image }}" alt="{{ post.name }}">
{% endfor %}
{% elif blogid='subdomain2' %}
{% set posts = blog_recent_posts('SUBDOMAIN2', 3) %}
{% for post in posts %}
    <h3><a href="{{ post.absolute_url }}">{{ post.name }}</a></h3>
    <img src="{{ post.featured_image }}" alt="{{ post.name }}">
{% endfor %}

and so on...
Reply
0 Upvotes
1 Reply 1
Advisor

@Blake32 you should be able to get that working using the HubL variable "request.domain" in your if statement. For example:

{% if request.domain is string_containing "www" %}
you're viewing a page with a WWW subdomain!
{% endif %}

 

--
Stefen Phelps
Web Developer / Co-founder
Kelp Creative Agency
Twitter — @stefen
Reply
0 Upvotes