CMS Development

sking1
Contributor

Combine multiple blogs in Related Posts

I want to combine 4 blogs into a Multiple related topics listing. This is my attempt to combine them:

<!-- combine blogs -->
{% set blog_one_posts = blog_recent_topic_posts('XXXXXX', topic.slug, 4) %} 
{% set blog_two_posts = blog_recent_topic_posts('XXXXXX', topic.slug, 4) %} 
{% set blog_three_posts = blog_recent_topic_posts('XXXXXX', topic.slug, 4) %} 
{% set blog_four_posts = blog_recent_topic_posts('XXXXXX', topic.slug, 4) %} 

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

This example is using the HS simple related post code:

{% if content.topic_list %}
<h3>Related posts</h3>
{% for topic in content.topic_list %}
{% if loop.first %}
{% set related_posts = blog_recent_topic_posts('default', topic.slug, 5 ) %}
{% for post in related_posts %}
{% unless content.absolute_url == post.absolute_url %}
{{ post.name}}
{% endunless %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %} 

I propose to change:

{% set related_posts = blog_recent_topic_posts(all_posts, topic.slug, 5 ) %}

But it's not working ...

 

 

0 Upvotes
2 Replies 2
dennisedson
HubSpot Product Team
HubSpot Product Team

Combine multiple blogs in Related Posts

hi @sking1

first, you do not need:

{% set related_posts = blog_recent_topic_posts('default', topic.slug, 5 ) %}

you already set up your array here:

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

second, you don't need the topic.slug in any of the setup.  I am assuming that you replaced the xxxxx's with the id's of the blogs

lastly, please note that the last number in the setup limits how many posts you get.  so when you are combining the blogs, you asked for 4 of each.  i only bring this up because later, when you define the related_posts, you used 5.  

 

<!-- combine blogs -->
{% set blog_one_posts = blog_recent_topic_posts('blogID' , 4) %} 
{% set blog_two_posts = blog_recent_topic_posts('blogID', 4) %} 
{% set blog_three_posts = blog_recent_topic_posts('blogID', 4) %} 
{% set blog_four_posts = blog_recent_topic_posts('blogID', 4) %} 

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

and the for loop:

{% for post in all_posts %}
// all of your content for the post structure goes here
{% endfor %}

here is an article detailing the process

sking1
Contributor

Combine multiple blogs in Related Posts

Thank you @dennisedson for the response. I got pulled into another project so this was delayed. I have figured out already how to combine posts together in a listing page, but what I am looking to do is to add a list of posts to the sidebar on a single post template with related tags. This describes how to generate related posts using multiple tags: https://designers.hubspot.com/tutorials/creating-a-related-post-section-with-hubl but it does not explain how to combine multiple blogs as well. What I am hung up on from the help article is how to incorporate multple blogs using 'all_posts' instead of a single 'default' blog:

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

 

0 Upvotes