Show the List of "Related articles" based on the Campaign

Occasional Contributor | Gold Partner

Hi there!

 

As the title suggest I'm trying to show the list of "Related articles" based on the Campaign on the Blog Post page but I'm having some trouble figuring out how to do that.

 

So for example, I have this campaign named Tutorial. If I open a blog post in that campaign it should display maximum six related blog posts. If there aren't any related articles then I want to show the most popular articles in that Topic

 

So in short, Total six related articles in the same campaign. If not available then most popular articles in the same topic.

 

Currently, I'm displaying articles based on the Topic. The code for it looks something like this.

 
{% if content.topic_list %}
               {% for topic in content.topic_list %}
                    {% if loop.first %}
                       {% set related_posts = blog_recent_topic_posts('default', topic.slug, 6 ) %}
                       {% for post in related_posts %}
                           {% unless content.absolute_url == post.absolute_url %}
                               <div class="recommended-post-item">
<!--                                     <div class="related-image">
                                       <a href="{{post.absolute_url}}"><img src="{{ post.featured_image }}" alt="{{ post.name }}"></a>
                                   </div> -->
                                   <div class="detail">
                                       <div class="related-title"><a href="{{post.absolute_url}}">{{ post.name}}</a></div>
<!--                                         <div class="date"> - {{ post.publish_date|datetimeformat('%B %e, %Y') }}</div> -->
                                   </div>
                               </div>
                           {% endunless %}
                       {% endfor %}
                   {% endif %}
               {% endfor %}
           {% endif %}   

 

It would be a huge help if anyone can help me figure this out. Thank you so much!

Reply
0 Upvotes
1 Reply 1
Advisor | Platinum Partner

Hi @awwaldesign,

 

For items inside of a campaign, there should be a HubL variable called 
{{ content.campaign_name }} that you can use. This would return the campaign name that is assigned. 

 

Your code should then have the following added in:

 

 

{% if content.topic_list %}
   {% for topic in content.topic_list %}
        {% if loop.first %}
          {% if content.campaign_name %}
              {% set related_posts = blog_recent_topic_posts('default', content.campaign_name, 6 ) %}
           {% else %}
              {% set related_posts = blog_popular_posts('default', topic.slug, 6 ) %}
           {% endif %}
           {% for post in related_posts %}
               {% unless content.absolute_url == post.absolute_url %}
                   <div class="recommended-post-item">
<!--                                     <div class="related-image">
                           <a href="{{post.absolute_url}}"><img src="{{ post.featured_image }}" alt="{{ post.name }}"></a>
                       </div> -->
                       <div class="detail">
                           <div class="related-title"><a href="{{post.absolute_url}}">{{ post.name}}</a></div>
<!--                                         <div class="date"> - {{ post.publish_date|datetimeformat('%B %e, %Y') }}</div> -->
                       </div>
                   </div>
               {% endunless %}
           {% endfor %}
       {% endif %}
   {% endfor %}
{% endif %}  

With this, you would have to make sure your campaign name is the same as your topic name or it won't pull anything. Also, wrapping it all in an if statement helps with fallback just in case. 

 

Note: You are also using recent post, there is a function for "related posts": https://designers.hubspot.com/docs/hubl/hubl-supported-tags#blog-related-posts

 

This might help more with showing relevant posts as opposed to recent. 

 

Hopefully, this helps point you in the right direction. 

-AJ

 

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

 

Reply
0 Upvotes