Featured post in HubSpot blog

Hello Developers,

Is it possible to show a featured post in HubSpot? My requirement is to highlight a post as a featured one which will not be changed automatically.

Actually I’ve used some HubL code before but it showed only the latest post as the featured post. So it’ll be changed every time we add new posts.

Thank you,
Shiyon

Hi, @shiyon :waving_hand: Thanks for reaching out. Let’s see if we can get the conversation going. I know @Stephanie-OG @dbeau79 @BarryGrennan @unicorndev have posted about or replied to this topic in the past.

Thank you for taking a look! —Jaycee

Hey @shiyon

The way I usually do this is to have a “featured” tag and then use the blog_recent_tag_posts function to get the latest post. If you just want one, you could do something like this:

{% set featured_posts = blog_recent_tag_posts("default", "featured", 1) %}

<div class="featured-post">
 <h1>{{ featured_posts[0].name }}</h1>
</div>

In the above case you’re setting the function to only get one post and then using the [0] index to fetch the first item on the list, but you could also use a loop like:

{% set featured_posts = blog_recent_tag_posts("default", "featured", 1) %}

{% for post in featured_posts %}
 <div class="featured-post">
 <h1>{{ post.name }}</h1>
 </div>
{% endfor %}

If you want to code it to be a specific post without using a tag (since you mention you don’t want it to be dynamic), you could also use the content_by_id function, like this:

{% set featured_post = content_by_id([[ content_id ]]) %}

<div class="featured-post">
 <h1>{{ featured_post.title }}</h1>
</div>

If you edit a post you’ll find the content id in the URL, the structure is usually something like https://app.hubspot.com/blog/123/editor/456/content where 123 is the blog id and 456 the content id.

You can also have it be a field in a custom module by adding a “Page” field in a custom module.

I hope that helps!

Hi @Stephanie-OG ,

Sorry for the late reply.

Thank you for the detailed explanation. I think the last one is the code I needed. I tried it inside my blog and it worked. But I found some practical issues to implement it.

1) Actually, this feature is controlled by our client who is not familiar with editing the template.

2) Is there any method to help client to select the blog post as featured?

3) Then we also need to hide it from the remaining list.

4) Can you please explain how to use the custom module with the ‘Page’ field? I tried to add it but it returned the blog id + post title.

Thanks

Shiyon

Hi @Stephanie-OG,

I got a partial, but effective solution from your following blog:

https://www.stephanieogaygarcia.com/hubspot-website-development/set-featured-posts-on-the-hubspot-blog-listings-page

When I implemented the code, I was able to see the post which is tagged as ‘featured’, the exact feature I want, but only one post is appearing in the Second loop. Could you please check the code?

{# First loop: featured posts #}
 {% for content in contents %}
 {% for topic in content.topic_list %}
 {% if topic.name == 'featured' %}
 <div class="post-item">
 {% if not simple_list_page %}
 <div class="post-header">
 <h2><a href="{{content.absolute_url}}">{{ content.name }}</a></h2>
 </div>
 <div class="post-body clearfix">
 <!--post summary-->
 {% if content.post_list_summary_featured_image %}
 <div class="hs-featured-image-wrapper">
 <a href="{{content.absolute_url}}" title="" class="hs-featured-image-link">
 <img loading="lazy"src="{{ content.post_list_summary_featured_image }}" class="hs-featured-image" alt="{{ content.featured_image_alt_text | escape }}">
 </a>
 </div>
 {% endif %}
 {{ content.post_list_content|safe }}
 </div>
 {% endif %}
 </div>
 {% endif %}
 {% endfor %}
{% endfor %}

{# Second loop: all other posts #}
{% for content in contents %}
 {% for topic in content.topic_list %}
 {% if topic.name != 'featured' %}
 <h2><a href="{{content.absolute_url}}">{{ content.name }}</a></h2>
 {% endif %}
 {% endfor %}
{% endfor %}

Thanks

Shiyon