HubL Reference

Table of contents
Close

Combining multiple sets of blog posts into a single listing

HubL functions allow designers to fetch sets of posts and print their variables on page templates. If you have multiple blogs, you can fetch sets of posts and mix them into a single sorted listing. Developers who are used to maintaining multiple post types on platforms like Wordpress and aggregating them into feeds can use the technique below to achieve a similar feature.

Please note that this technique does not support pagination. Pagination settings in HubSpot will only apply to standard blog listings. If you wanted to paginate content rendered with a HubL function, you would need to use Javascript to paginate the static set of posts.

This example will combine the 5 most recent posts from two distinct blogs into a single listing. This technique can be used on any page template. To implement this technique:

  • Define a variable and store a set of posts
  • Define a second variable that stores a second set of posts 
  • Define a third variable that combines the two variables and applies a sort filter. The example below sorts by publish date, but you could also sort based on other properties such as 'name'.
  • Loop through the posts in this combined variable
  • Print any blog variables within your loop*

{% set blog_one_posts = blog_recent_posts('default', limit=5) %} <!-- sets a variable for a the 5 most recent posts of my default COS blog -->
{% set blog_two_posts = blog_recent_posts('47082700', limit=5) %}  <!-- sets a variable for a the 5 most recent posts of my blog id 47082700. You can find the blog id in the URL of the blog dashboard for a particular blog-->
{% set all_posts = (blog_one_posts + blog_two_posts) | sort(true, false, 'publish_date')  %} <!-- combines the two variables into a single variable and sorts them by publish date. Please note the sort filter requires all three parameters -->
{% for post in all_posts %} <!-- loop through posts in custom all posts variable -->
                        <div class="post-item">
                            {% if not simple_list_page %}
                                <div class="post-header">
                                    <h2><a href="{{post.absolute_url}}">{{ post.name }}</a></h2>
                                    <div id="hubspot-author_data" class="hubspot-editable" data-hubspot-form-id="author_data" data-hubspot-name="Blog Author">
                                        Posted by
                                        {%if post.blog_post_author %}
                                            <a class="author-link" href=" {{ post.parent_blog.absolute_url }}/author/{{ post.blog_post_author.slug }}">{{ post.blog_post_author.display_name }}</a> on {{ post.publish_date_localized }}
                                        {% endif %}
                                    </div>
                                    {% blog_social_sharing "blog_social_sharing" overrideable=False, label='Blog Social Sharing' %}
                                </div>
                                <div class="post-body clearfix">
                                    <!--post summary-->
                                    {% if post.post_list_summary_featured_image %}
                                        <div class="hs-featured-image-wrapper">
                                            <a href="{{post.absolute_url}}" title="" class="hs-featured-image-link">
                                                <img src="{{ post.post_list_summary_featured_image }}" class="hs-featured-image">
                                            </a>
                                        </div>
                                    {% endif %}
                                    {{ post.post_list_content|safe }}
                                </div>
                                {% if content_group.show_summary_in_listing %}
                                    <a class="more-link" href="{{ post.absolute_url }}">Read More</a>
                                {% endif %}
                                <div class="custom_listing_comments">
                                    {% set comments_number =  post.comment_list|length %}
                                    {% set comments_label = "Comment" if comments_number == 1 else "Comments" %}
                                    {{ comments_number }} {{ comments_label }} <a href="{{post.absolute_url}}#comments-listing">Click here to read/write comments</a>
                                </div>
                                {% if post.topic_list %}
                                     <p id="hubspot-topic_data"> Topics:
                                        {% for topic in post.topic_list %}
                                            <a class="topic-link" href=" {{ post.parent_blog.absolute_url }}/topic/{{ topic.slug }}</a>{% if not loop.last %},{% endif %}
                                        {% endfor %}
                                     </p>
                                {% endif %}
                            {% else %}
                                <h2 class="post-listing-simple"><a href="{{post.absolute_url}}">{{ post.name }}</a></h2>
                            {% endif %}
                        </div>
{% endfor %}

*The code within the for loop is a modified version of HubSpot's standard listing, where "content" has been replaced with "post" so that the various variables are in relation to the custom set of aggregated posts.