When using the related blog posts HubL tag in the blog post template, is it possible to use the 'tags' parameter to display a selection of blog posts that share one or all of the same values as those used by a given blog post the user is viewing? And, can they be displayed in order of most views? What would that look like?
Feb 27, 202012:37 AM - edited Jul 22, 20221:49 PM
Recognized Expert | Elite Partner
Related blog posts HubL tag
SOLVE
The related_blog_posts function works a little differently from other blog post listing functions in that it uses a macro to format the posts instead of a for loop.
<!-- related blog posts section starts here -->
<div class="row-fluid ">
<div class="span12 widget-span widget-type-raw_jinja " style="" data-widget-type="raw_jinja" data-x="0" data-w="12">
{% macro blog_post_formatter(post) %}
<div class="span4">
<div class="post-card {% for topic in post.topic_list %}{{ topic.slug }}{% if not loop.last %} {% endif %} {% endfor %} bh-ver">
<div class="bh-hor">
<a href="{{ post.absolute_url }}">
<div class="ft-image" style="background: url('{{ post.featured_image }}') center no-repeat;">
</div>
<div class="content">
<h5>{{ post.name }}</h5>
<p>
{{ post.publish_date|datetimeformat('%B %e, %Y') }}
</p>
</div>
</a>
</div>
</div>
</div>
{% endmacro %}
{% set post_tags = content.tagList|join(", ") %}
{% related_blog_posts limit=3, tags="{{ post_tags }}" post_formatter="blog_post_formatter" %}
</div>
<!-- end of blog related section -->
You can read more about this function in one of my blog posts -
Here is latest code for related post, It will not appear current post in current post, I have checked other comment they are using old code.
{% if content.topic_list %} {% set max_posts = 3 %}<!-- 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(group.id, topic.slug, max_posts + 1 ) %} {% if loop.last %} {% set post_list = post_list|sort(true, false, 'publish_date')|unique('name') %} {% set i = 0 %}
<h2>{{ topic|replace('-',' ') }}</h2> <!-- It will Show the Topic Name--> {% for post in post_list %} {% if content.absolute_url != post.absolute_url and i < max_posts %} <li> <a href="{{post.absolute_url}}"> <span class="crp_title">{{ post.name}}/span> </a> </li> {% set i = i + 1 %} {% endif %} {% endfor %} {% endif %} {% endfor %} </ul> {% endif %}
The tags filter in related_blog_posts gives posts with the listed tags more relevancy. So posts with each of the tags would definitely have a higher chance of being listed, but there's no certainty that only posts with ALL the listed tags will be the ones shown.
If you want to grab the tags from the current post to add to your related_blog_posts function it would looks something like this :
For your question of sorting them by most views--there is no option in the related_blog_posts function to do so (it would certainly be a great addition though and I suggest submitting an idea to the Ideas Forum for it). I believe only blog_popular_posts has that functionality and it can only be filtered down to a singular tag slug, not multiple.
{% set post_tag = [] %}
{% for topic in content.topic_list|first %}
{% set post_tags = post_tag.append(topic.slug) %}
{% endfor %}
{% set pop_posts = blog_popular_posts('default', 3, post_tag[0]) %}
{% for pop_post in pop_posts %}
<h2>{{ post_post.name }}</h2>
{% endfor %}
If this answer solved your question, please mark it as the solution.
I've tried adding your code to the blog post template code but it won't let me publish it. I'm getting an error. Any ideas as to what the reason could be? Here is a copy of the code and I've marked the related blog posts part in red comments.
{% choice "optional_CTA" label="<span style=color:#e8771b;>Do you want to show the optional call-to-action?</span><br><span>Hit Save to refresh the preview.</span>", choices="No,Yes", export_to_template_context=True %} {% text "call_to_action" label="Call to action", export_to_template_context=True %} {% cta "cta" label="CTA button", export_to_template_context=True %}
Feb 27, 202012:37 AM - edited Jul 22, 20221:49 PM
Recognized Expert | Elite Partner
Related blog posts HubL tag
SOLVE
The related_blog_posts function works a little differently from other blog post listing functions in that it uses a macro to format the posts instead of a for loop.
<!-- related blog posts section starts here -->
<div class="row-fluid ">
<div class="span12 widget-span widget-type-raw_jinja " style="" data-widget-type="raw_jinja" data-x="0" data-w="12">
{% macro blog_post_formatter(post) %}
<div class="span4">
<div class="post-card {% for topic in post.topic_list %}{{ topic.slug }}{% if not loop.last %} {% endif %} {% endfor %} bh-ver">
<div class="bh-hor">
<a href="{{ post.absolute_url }}">
<div class="ft-image" style="background: url('{{ post.featured_image }}') center no-repeat;">
</div>
<div class="content">
<h5>{{ post.name }}</h5>
<p>
{{ post.publish_date|datetimeformat('%B %e, %Y') }}
</p>
</div>
</a>
</div>
</div>
</div>
{% endmacro %}
{% set post_tags = content.tagList|join(", ") %}
{% related_blog_posts limit=3, tags="{{ post_tags }}" post_formatter="blog_post_formatter" %}
</div>
<!-- end of blog related section -->
You can read more about this function in one of my blog posts -
I tried out the macro code that @alyssamwilie provided and it worked. Thanks a lot! Three related blog posts are displaying at the end of each blog post and they're being matched to the current blog's tags.