CMS Development

Jools619
Participant

Creating a relevant Blog list by Tags

SOLVE

Hi 

 

When someone clicks on an indivual blog page to read, at the bottom is a list where I would like 3 related posts to appear based on the tag or tags of the blog page im currently reading. 

 

Ive found a few guides, but they all rely on setting the name of the tag(s) beforehand.

 

I wont know this so I'll need to somehow check the for tags each time on which ever blog story i click, then spit out 3 relevent blogs matching the tag. And I only have the one template page for this so its not as if i have a different page for each of the tags that we have.

 

Any information much appreciated

 

Thanks

 

0 Upvotes
1 Accepted solution
Jools619
Solution
Participant

Creating a relevant Blog list by Tags

SOLVE

That's awesome works exactly how I wanted it to.

Thanks jzilch 🙂

View solution in original post

0 Upvotes
2 Replies 2
jzilch
HubSpot Employee
HubSpot Employee

Creating a relevant Blog list by Tags

SOLVE

Hi, 

 

Normally we use this sort of HubL below. 

{% if content.topic_list %}
  <h3>Related posts</h3>
  {% set max_posts = 4 %}<!-- Set the max number of related posts to be output to the page here -->
  {% set post_list = [] %}
  {% for topic in content.topic_list %}
    {% set post_list = post_list + blog_recent_topic_posts('default', topic.slug, max_posts + 1 ) %}
    {% if loop.last %}
      {% set post_list = post_list|sort(true, false, 'publish_date')|unique('name') %}
      {% set i = 0 %}
      {% for post in post_list %}
        {% if content.absolute_url != post.absolute_url and i < max_posts %}
          <div class="related-post-item">
            <div class="related-image"><img src="{{ post.featured_image }}" alt="{{ post.name }}"></div>
            <div class="related-title"><a href="{{post.absolute_url}}">{{ post.name}}</a></div>
          </div>
          {% set i = i + 1 %}
        {% endif %}
      {% endfor %}
    {% endif %}
  {% endfor %}
{% endif %}

This should keep you from having to manually set which of the blog tags you want to have rendered at the bottom of the posts. 

 

Let me know if this helps

 

 

 

 

 

 

 

 

 

Jools619
Solution
Participant

Creating a relevant Blog list by Tags

SOLVE

That's awesome works exactly how I wanted it to.

Thanks jzilch 🙂

0 Upvotes