CMS Development

sking1
Contributor

Exclude blog topics from Related Post

As a workaround to the lack of hierarchical tags, I created many tags to sort and filter content, which I hide on the front end. Each tag begins with a label such as Queue, Product, Audience, Menu, Topic, and Vertical.

 

Our marketing team wants Related Posts to only include tags that start with Topic and Vertical.

 

Following the instructions for Multiple related topics listing, I believe I need to apply a filter to topic.slug, below:

{% set topic_one = blog_recent_topic_posts('default', topic.slug, max_posts + 1 ) %}

I tried this, but it is obviously incorrect:

 

{% set my_topics = topic.slug is string_startingwith "vertical" or topic.slug is string_startingwith "topic" %}

And then swapped my_topics for topic.slug 

{% set topic_one = blog_recent_topic_posts('default', my_topics, max_posts + 1 ) %}

 I would appreciate any insight how to accomplish this.

0 Upvotes
3 Replies 3
AJLaPorte_diagr
Key Advisor

Exclude blog topics from Related Post

Hey @sking1,

 

Have you tried using the "Blog Related Posts" tag? I did a writeup on this and have sample code on how to do something similar dynamically on a blog post page with using author instead of tag. But you could still use the same idea and pull in the blog topic names.

https://blog.wsol.com/in-beta-hubspots-related-posts-tag-explained-and-extended

 

real quick code example:

{% related_blog_posts limit=2, blog_ids="default", tags="Sales enablement,Marketing" %}

for the tags param, you could create a custom module and use the "Tag Selector" field so users can choose them that way, then just pop those values into that param so you would have something like:

{% related_blog_posts limit=2, blog_ids="default", tags="{{field_var.values}}" %}

Hopefully, this helps to answer your question. If you need more clarification, please let me know. 

-----------------------
AJ LaPorte
www.wsol.com

sking1
Contributor

Exclude blog topics from Related Post

Thank you @AJLaPorte_diagr for the response, I have to create a filter first to pull out tags I do not want to show in the Related Posts. I did so here:

 

 

{% for topic in content.topic_list %}
{% if topic.name is string_startingwith "Queue" %}
{% elif topic.name is string_startingwith "Menu" %}
{% elif topic.name is string_startingwith "Product" %}
{% elif topic.name is string_startingwith "Audience" %}
{% else %}

<!-- combine blogs -->
{% set blog_one_posts = related_blog_posts('1111111111', tags='Topic Cj 101, Topic Holiday, Topic Strategy', limit="5") %} <!-- video -->
{% set blog_two_posts = related_blog_posts('2222222222', tags='Topic Cj 101, Topic Holiday, Topic Strategy', limit="5") %} <!-- article -->
{% set blog_three_posts = related_blog_posts('3333333333', tags='Topic Cj 101, Topic Holiday, Topic Strategy', limit="5") %} <!-- infographic -->
{% set blog_four_posts = related_blog_posts('4444444444', tags='Topic Cj 101, Topic Holiday, Topic Strategy', limit="5") %} <!-- report -->

{% set all_posts = (blog_one_posts + blog_two_posts + blog_three_posts + blog_four_posts) %}

{% endif %}
{% endfor %}

 

I plan on swapping the tags="" out with tags="{{ topic.name }}{% if not loop.last %},{% endif %}" after I make sure the hard coded tags work first. 

 

Then, I used the HubSpot provided Related Tags hubl. I swapped "default" for "all_posts"

{% set topic_one = blog_recent_topic_posts('all_posts', topic.slug, max_posts + 1 ) %}

 

{% set max_posts = 5 %} <!-- max # of related posts -->
{% set count = 0 %}
{% for topic in content.topic_list %}
{% if loop.index == 1 %}
{% set topic_one = blog_recent_topic_posts('all_posts', topic.slug, max_posts + 1 ) %}
{% if topic_one|length == 1 %}
{% endif %}
{% elif loop.index == 2 %}
{% set topic_two = blog_recent_topic_posts('all_posts', topic.slug, max_posts + 1 ) %}
{% elif loop.index == 3 %}
{% set topic_three = blog_recent_topic_posts('all_posts', topic.slug, max_posts + 1 ) %}
{% endif %}
{% if loop.length >= 3 and loop.index == 3 %}
{% set related_posts = (topic_one + topic_two + topic_three)|sort(true, false, 'publish_date')|unique('name') %}
{% set loop_count = 3 %}
{% elif loop.length == 2 and loop.index == 2 %}
{% set related_posts = (topic_one + topic_two)|sort(true, false, 'publish_date')|unique('name') %}
{% set loop_count = 2 %}
{% elif loop.length == 1 and loop.index == 1 %}
{% set related_posts = topic_one %}
{% set loop_count = 1 %}
{% endif %}
{% if loop.index == loop_count %}
{% for post in related_posts %}
{% if content.absolute_url != post.absolute_url and count < max_posts %}

<ul class="list-unstyled">
<div class="media {% if loop.last %}pb-0{% else %}pb-3{% endif %}">
<a href="{{post.absolute_url}}"><div class="bg-image image mr-3" style="background-image: url('{{ post.featured_image }}');"></div></a>
<div class="media-body">
{% if post.topic_list %}
<div class="text-teal font-supheader mb-2">
{% for topic in post.topic_list %}
{% if topic.name is string_startingwith "Queue" %}
{% elif topic.name is string_startingwith "Menu" %}
{% elif topic.name is string_startingwith "Product" %}
{% elif topic.name is string_startingwith "Audience" %}
{% else %}
<a class="text-teal" style="text-decoration:none" href="/filter?topic={{ topic.slug }}">{{ topic.name|cut('Vertical ')|cut('Topic ') }}</a>{% if not loop.last %},{% endif %}
{% endif %}
{% endfor %}
</div>
{% endif %}
<a class="font-body-lg" href="{{post.absolute_url}}" style="text-decoration:none">{{ post.name }}</a>
</div>
</div>
</ul>
{% set count = count + 1 %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}

 

0 Upvotes
JanetArmstrong
Top Contributor | Partner
Top Contributor | Partner

Exclude blog topics from Related Post

You can use the RSS Listing module to show tag specific blog posts as explained here:

 

https://knowledge.hubspot.com/articles/kcs_article/cos-blog/how-do-i-display-tag-specific-blog-posts...