CMS Development

tommitchell
Contributor

Display only blog posts of a single tag

SOLVE

Hello, I have a recent posts module set up as below but I need it to only pull in posts with the tag 'For landlords' and no others. Is this possible please and if so could someone advise how to update the code to achieve this?

 

Thanks so much

Tom

 

 

{% set recent_posts = blog_recent_posts('5260207072', 3) %}
<div class="recent-section1">
{% for recent_post in recent_posts %}
<div class="recent-box">
<a href="{{ recent_post.absolute_url }}"><div class="recent-image" style="background-image:url('{{ recent_post.featured_image }}')">
<!--<img src="{{ recent_post.featured_image }}" alt="feature image"/>-->
<div class="recent-date">{{ recent_post.publish_date|datetimeformat('%e %B %Y') }}</div>
</div></a>
<div class="recent-text">
<div class="recent-title">{{ recent_post.name }}</div>
<!-- <div class="recent-content"> {{ recent_post.post_body|striptags|truncatehtml(200) }} </div> -->
<!--<a class="link-style" href="{{ recent_post.absolute_url }}" title="Read More"> Read More </a> -->
<a class="recent-title" style="line-height: 32px; font-family: 'BrandonText-Bold', sans-serif; text-transform: uppercase;" href="{{ recent_post.absolute_url }}" title="Read more"> Read more... </a>
</div>
</div>
{% endfor %}
</div>

0 Upvotes
1 Accepted solution
lscanlan
Solution
HubSpot Alumni
HubSpot Alumni

Display only blog posts of a single tag

SOLVE

Hi @tommitchell,

 

Do you need posts that have exactly that 1 tag? Or do you just need the most recently published posts that have that tag? If you need just the most recently published posts that have that tag, that's a pretty quick fix actually. You're currently pulling in the most recently published posts (regardless of tags) with the blog_recent_posts() function (documented here: https://designers.hubspot.com/en/docs/hubl/hubl-supported-functions#blog-recent-posts😞

 

{% set recent_posts = blog_recent_posts('5260207072', 3) %}

But you could change that to use the blog_recent_tag_posts() function (documented here: https://designers.hubspot.com/en/docs/hubl/hubl-supported-functions#blog-recent-tag-posts). So you'd change only that one line of code to this:

 

{% set recent_posts = blog_recent_tag_posts('5260207072', tag-slug-goes-here, 3) %}

And you'll just need to swap out "tag-slug-goes-here" for the actual slug for the topic you want to use. So for example if you're looking for posts that have a tag of "For landlords", the listing URL for that topic would look something like: https://www.domain.com/blog/tag/for-landlords. So your blog_recent_tag_posts() function would look like: {% set recent_posts = blog_recent_tag_posts('5260207072', for-landlords, 3) %}

 

And that's all you'll need to change. You're not changing the name of the list that you're generating from your function. So only the posts that get pulled in will change.

 

Leland Scanlan

HubSpot Developer Support

View solution in original post

0 Upvotes
2 Replies 2
lscanlan
Solution
HubSpot Alumni
HubSpot Alumni

Display only blog posts of a single tag

SOLVE

Hi @tommitchell,

 

Do you need posts that have exactly that 1 tag? Or do you just need the most recently published posts that have that tag? If you need just the most recently published posts that have that tag, that's a pretty quick fix actually. You're currently pulling in the most recently published posts (regardless of tags) with the blog_recent_posts() function (documented here: https://designers.hubspot.com/en/docs/hubl/hubl-supported-functions#blog-recent-posts😞

 

{% set recent_posts = blog_recent_posts('5260207072', 3) %}

But you could change that to use the blog_recent_tag_posts() function (documented here: https://designers.hubspot.com/en/docs/hubl/hubl-supported-functions#blog-recent-tag-posts). So you'd change only that one line of code to this:

 

{% set recent_posts = blog_recent_tag_posts('5260207072', tag-slug-goes-here, 3) %}

And you'll just need to swap out "tag-slug-goes-here" for the actual slug for the topic you want to use. So for example if you're looking for posts that have a tag of "For landlords", the listing URL for that topic would look something like: https://www.domain.com/blog/tag/for-landlords. So your blog_recent_tag_posts() function would look like: {% set recent_posts = blog_recent_tag_posts('5260207072', for-landlords, 3) %}

 

And that's all you'll need to change. You're not changing the name of the list that you're generating from your function. So only the posts that get pulled in will change.

 

Leland Scanlan

HubSpot Developer Support
0 Upvotes
tommitchell
Contributor

Display only blog posts of a single tag

SOLVE

Hi @lscanlan  - thanks so much for your help, really appreciated!

Tom

0 Upvotes