Hola! ¡Tenemos nuestra Comunidad en Español!

Automatic Related Posts based on Blog Tags

Highlighted
Occasional Contributor

Hi, I'm wondering where I've went wrong trying to implement the related posts HubL on my site. What I need is for up to 6 related blog posts to populate automatically on a single blog post, based on that posts' tags. It should be able to use the post tags, and pull in other related blog posts. I've created a module, but nothing is being pipulated at this time. Here's my code: 

 

<section class="related">
  <h3>
    Related Products
  </h3>
  <div class="post-listing">
   {% macro blog_post_formatter(blog_post) %}
      <a href="{{blog_post.url}}">
        <div class="wrapper" style="background: #fff url('{{ blog_post.featured_image_url }}') no-repeat top center / 100% auto;">
          <h4>
            {{blog_post.name}}
          </h4>
          {% if blog_post.post_summary|length %}
            <p>
              {{ blog_post.post_summary|safe }}
            </p>
          {% endif %}
        </div>
      </a>
    {% endmacro %}
    
    {% set tagNames = content.tag_names %}

	  {% related_blog_posts blog_ids="1234567" limit="6", tags="{{tagNames|join(',')}}", start_date="{{year - 1}}-01-01", post_formatter="blog_post_formatter"  %}
  
  </div>
</section>

I've veen tried the most simple implementation in the tutorials, and still, nothing gets pulled in. Please help! 

5 Replies
HubSpot Employee

Hi Maribeth, I'm not sure if you've solved this by now, but I figured I'd share my thoughts...

 

The issue I'm seeing is how the Related Posts tag is being called:

{% related_blog_posts blog_ids="5071829" limit="6", tags="{{tagNames|join(',')}}", start_date="{{year - 1}}-01-01", post_formatter="blog_post_formatter"  %}

Whenever you are within a HubL declaration ( {% ... %} ), you do not need to use {{ }} to reference HubL variables, you can just write the variables without the {{ }}. Also the 'limit' attribute value does not need to be in quotes:

{% related_blog_posts blog_ids="5071829" limit=6, tags=tagNames|join(','), start_date=(year - 1)|string~"-01-01", post_formatter="blog_post_formatter"  %}

Here is HubSpot's article on using the Blog Related Posts tag, which has some examples of different uses of the tag - https://designers.hubspot.com/tutorials/creating-a-related-post-listing

 

Hope this helps!

Reply
0 Upvotes
Occasional Contributor

Hi James, 

 

Thanks for your reply! I updated my code to the correct syntax, however I'm still not seeing any related posts. Smiley Sad 

 

Here's my updated code:

{% set tagNames = content.tag_names %}
{% related_blog_posts blog_ids="5071829", limit=6, tags=tagNames|join(','), post_formatter="blog_post_formatter" %}

 

I know there should be a few related posts rendered based on tags, so I'm not sure why they're not showing up. Do you have any other thoughts? 

 

Thanks! 

Reply
0 Upvotes
HubSpot Employee

Hi MaryBeth,

 

Okay, at least we've ruled out the syntax causing the issue...


Looking at the blog ID you're using, this is me grasping at straws, but I'm wondering if that's not the correct ID? Usually I'd expect it to be a longer number like '5900440271'. For reference, the ID of a blog can be found in the URL of the blog dashboard: 

https://app.hubspot.com/blog/[PORTAL ID]/blogs/[BLOG ID]/manage/posts/all

 

Additionally, you're assigning the 'tagNames' variable the value of 'content.tag_names' - if you are not viewing this module in the context of a blog post (previewing the module in the design manager, for example), this variable would not be set properly.

 

Hopefully this gets us a bit further, let me know if I need to clarify anything.

 

Best,
James

Reply
0 Upvotes
Occasional Contributor

Hi James, 

 

The blog post ID fixed one issue - so now it is pulling in the correct posts, however, it isn't using the macro that I set and it's just using the default markup when it prints the related posts. 

 

Any thoughts on that? 

 

Thank you so much for your help! 

Reply
0 Upvotes
HubSpot Employee

Hi Maribeth,

 

Sounds like we're making progress! 

 

I wonder if the way you've defined your macro is causing it to not be usable by the related posts tag. Your macro takes one argument:

{% macro blog_post_formatter(blog_post) %}

but the custom macro for the related posts tag is supposed to take three arguments:

{% macro blog_post_formatter(blog_post, index, count) %}

I wonder if the tag is rejecting your macro and falling back to using the default formatter, because it is unable to call your macro using the 'index' and 'count' arguments.

Reply
0 Upvotes