CMS Development

sdehoogh
Participant

Blog RSS feed with multiple tags

SOLVE

Community,

 

We have been trying to build a custom module that shows blog articles on a website page. based on their tag. The module must only show an article when it has both tags included, for example: an article is both a case study (tag 1) and for a specific industry (tag 2), so it is shown in the list. 

 

The basic code we have built uses two tag selector menu's but does not work. It does however work on only a single tag. We have checked the manuals and tutorials but cannot find an answer. 

 

Does anyone have an answer? Thanks in advance!!

 

The HubL code we have now is:

 

<div class="ep-page-center {{ module.custom_css_class }}">
{% set tag_posts = blog_recent_tag_posts ('default', module.tag_field and module.tag_field_1, limit=3) %}
{% for tag_post in tag_posts %}
<div class="ep-blog-container">
<div class="ep-post-item {{tag_post.name}}">
<a href="{{tag_post.absolute_url}}">
<div style="background-image: url({{tag_post.featured_image}}" class="ep-blog-featured-image"></div>
</a>
<div class="ep-post-content">
<a href="{{tag_post.absolute_url}}">{{tag_post.name}}</a>
<p>{{tag_post.post_summary|truncatehtml(200)}}</p>
<a href="{{tag_post.absolute_url}}">Read More</a>
</div>
</div>
</div>
{% endfor %}
</div>

0 Upvotes
1 Accepted solution
lscanlan
Solution
HubSpot Alumni
HubSpot Alumni

Blog RSS feed with multiple tags

SOLVE

Hi @sdehoogh,

 

I think I figured this one out. It's a little complicated so I've tried to indent, space, and comment my code, along with naming variables semantically. My module will pull a list of blog posts (up to 100) that contain both tag A and tag B. It needs 3 fields:

 

  • A blog field that I've named blog_field . This will output the ID of the blog.
  • A text field that I've named tag_one_slug . This will output the tag slug as a string.
  • A text field that I've named tag_two_slug . This will output the tag slug as a string.

 

It sounds like you're using a choice field, which is also fine. You'd just need to adjust the code a bit to work with that field type. And then here's my code:

 

{# create two lists: one for posts that have either tag, one for posts that have both tags #}
{% set posts_with_either_tag = blog_recent_tag_posts(module.blog_field, [module.tag_one_slug, module.tag_two_slug], 100)|sort(false, false, 'publish_date') %}
{% set posts_with_both_tags = [] %}

{# loop through the list of posts with either tag one or tag two #}
{% for post in posts_with_either_tag %}

	{# loop through the tags in the current post's tag list #}
	{% for tag in post.tag_list %}

		{# ultimately we need to find both tags in a post's tag list #}
		{# so we start by finding posts that have the 1st tag #}
		{% if tag.slug == module.tag_one_slug %}

			{# this post has the 1st tag #}
			{# so right inside the tag list loop we loop through the tag list again #}
			{% for tag in post.tag_list %}

				{# now we're looking for the 2nd tag #}
				{% if tag.slug == module.tag_two_slug %}

					{# this post has both tags #}
					{# we append it to the list posts_with_both_tags #}
					{% do posts_with_both_tags.append(post) %}

				{% endif %} {# end if tag.slug == module.tag_two_slug #}

			{% endfor %} {# end for tag in post.tag_list #}

		{% endif %} {# end if tag.slug == module.tag_one_slug #}

	{% endfor %} {# end for tag in post.tag_list #}

{% endfor %} {# end for tag_post in tag_posts #}

<div class="post-wrapper" style="border: 1px solid black;">
	{% for post in posts_with_both_tags %}
  	<p>post.name: {{ post.name }}</p>
  {% endfor %}
</div>

I'm sure there are other ways of accomplishing this, but I think this will do what you're looking for. I hope it helps. And let me know if you have any questions.

 

Leland Scanlan

HubSpot Developer Support

View solution in original post

3 Replies 3
lscanlan
Solution
HubSpot Alumni
HubSpot Alumni

Blog RSS feed with multiple tags

SOLVE

Hi @sdehoogh,

 

I think I figured this one out. It's a little complicated so I've tried to indent, space, and comment my code, along with naming variables semantically. My module will pull a list of blog posts (up to 100) that contain both tag A and tag B. It needs 3 fields:

 

  • A blog field that I've named blog_field . This will output the ID of the blog.
  • A text field that I've named tag_one_slug . This will output the tag slug as a string.
  • A text field that I've named tag_two_slug . This will output the tag slug as a string.

 

It sounds like you're using a choice field, which is also fine. You'd just need to adjust the code a bit to work with that field type. And then here's my code:

 

{# create two lists: one for posts that have either tag, one for posts that have both tags #}
{% set posts_with_either_tag = blog_recent_tag_posts(module.blog_field, [module.tag_one_slug, module.tag_two_slug], 100)|sort(false, false, 'publish_date') %}
{% set posts_with_both_tags = [] %}

{# loop through the list of posts with either tag one or tag two #}
{% for post in posts_with_either_tag %}

	{# loop through the tags in the current post's tag list #}
	{% for tag in post.tag_list %}

		{# ultimately we need to find both tags in a post's tag list #}
		{# so we start by finding posts that have the 1st tag #}
		{% if tag.slug == module.tag_one_slug %}

			{# this post has the 1st tag #}
			{# so right inside the tag list loop we loop through the tag list again #}
			{% for tag in post.tag_list %}

				{# now we're looking for the 2nd tag #}
				{% if tag.slug == module.tag_two_slug %}

					{# this post has both tags #}
					{# we append it to the list posts_with_both_tags #}
					{% do posts_with_both_tags.append(post) %}

				{% endif %} {# end if tag.slug == module.tag_two_slug #}

			{% endfor %} {# end for tag in post.tag_list #}

		{% endif %} {# end if tag.slug == module.tag_one_slug #}

	{% endfor %} {# end for tag in post.tag_list #}

{% endfor %} {# end for tag_post in tag_posts #}

<div class="post-wrapper" style="border: 1px solid black;">
	{% for post in posts_with_both_tags %}
  	<p>post.name: {{ post.name }}</p>
  {% endfor %}
</div>

I'm sure there are other ways of accomplishing this, but I think this will do what you're looking for. I hope it helps. And let me know if you have any questions.

 

Leland Scanlan

HubSpot Developer Support
YChuskit
Participant | Elite Partner
Participant | Elite Partner

Blog RSS feed with multiple tags

SOLVE

hey @lscanlan ,

if i want to filter my blog with either tag1 or tag 2,can i achieve that using this same code ? i tried using it but  some how it is showing me the blogs with tag1 only . in place of text field i used tag field like @sdehoogh  did. 

0 Upvotes
sdehoogh
Participant

Blog RSS feed with multiple tags

SOLVE

Thanks Leland, this works indeed! Excellent solution, can recommend to others who want to achieve the same.

0 Upvotes