Showing different modules in blog template code, based on authors or based on blog tag

Hi,

A colleague of mine asked whether we could display one text block at the bottom of some blog posts, and a different text block at the bottom of other blog posts. I’d like to do this within the template code to automatically have these messages come through to all blogs.
The only way I can think of, or maybe the simplest way to do so would be an IF/ELSE STATEMENT that shows different <div> 's based on the author, or based on the blog tag. I’m thinking something like the below:

{% if blog_author == Louise %}
<div>
<p> Message for Louise’s blogs </p>
</div>
{% else %}

<div><p> a different message <p></div>
{% endif %}

Putting the above at the bottom of the blog post template shows the first message on all blogs though.

Any ideas what I’m doing wrong… or how I can target the blog tag?

Thanks in advance

Yes you can do this. The variables for blog author is content.blog_post_author.display_name and for blog tags it’s content.topic_list. The topic list is a dictionary (i.e. list) because you can have a blog with multiple tags so you’ll have to loop over it.

Author:

{% if content.blog_post_author.display_name == 'Louise' %}
 <p>Message for Louise's blogs</p>
{% else %}
 <p>A different message</p>
{% endif %}

Tags:

{% for tag in content.topic_list %}
 {% if tag == 'Dogs' %}
 <p>This is a message for blog posts about dogs</p>
 {% endif %}
{% endfor %}

Edit: changed last endif for endfor, my bad,

Hi Piers,

Thanks so much - the IF for author is working perfectly.
I’ll probably use the if statement based on the author anyway, but there was problem publishing the statement based on tags:

Thanks again for your quickly reply and solutions.

Ryan

@RKneller ah sorry, just a small typing error on my part. Should be

{% for tag in content.topic_list %}
 {% if tag == 'Dogs' %}
 <p>This is a message for blog posts about dogs</p>
 {% endif %}
{% endfor %}

Amazing Piers thanks!

Hi @piersg, thanks very much for your help previously. I have been using this bit of code successfully since November now!

However, I’m now looking to add another layer of complexity to the above. I’d like to display one CTA if the blog has a tag of “recruiting”, else display a different CTA based on author.
I wondered if I could please ask for your help writing a statement that would help me do that?
I tried the below, but had to wrap the if statement for the tag in " {% for %} and {% endfor %}" as you suggested.

This works, but obviously I now have two different if statements, and my blog post displays one CTA when the tag is “recruiting” and another below it based on the outcome of the author.
Is there a way to combine these two “if statements” into one, or is that trying to combine two different fields in different tables?
I understand there may be a scenario where there could be two CTAs if the statement determines that a tag is in use, and that the author is also matched.
Thanks so much.

Ryan

Hi @RKneller, yes that’s possible. Just to make sure I’ve understood: if the blog post has the “recruiting” tag you want to display the relevant module, otherwise display a different module based on the blog author.

The logic here is to first check if recruiting exists using, that for loop from before, and update a variable outside of said for loop with “true” (in this case the variable is an array and we’re adding true to that array as you can’t actually update normal variables outside of a for loop with HubL). Then it’s just using a normal if statement, first checking for whether true exists in the variable, otherwise displaying different modules based on author name.

{% set recruiting = [] %}
{% for tag in content.topic_list %}
 {% if tag == 'Recruiting' %}
 {% do recruiting.append(true) %}
 {% endif %}
{% endfor %}

{% if true in recruiting %}
	{# recruit module here #}
{% elif content.blog_post_author.display_name == 'Louise Otton' %}
	{# coaching module here #}
{% elif content.blog_post_author.display_name == 'Megan O Hara' %}
	{# emplyoment module here #}
{% else %}
	{# default module here #}
{% endif %}

@piersg, thanks very much again. Really appreciate this - that works perfectly and can confirm the “recruiting” tag takes precedence over the author, when both are used :slightly_smiling_face: