Displaying Featured Posts First in A Hubspot Recent Posts Tag
Are you looking to showcase a featured blog post prominently in a Hubspot Recent Posts function? It's a great way to draw attention to important content and engage your readers. In this blog post, we will provide a step-by-step guide. By following these instructions, you'll be able to display your featured blog post first, followed by the rest of your recent posts as normal. Let's dive in!
Here we'll assume that we want to show 6 posts and we want the first one to be the featured one. So we need to search through the most recent posts to avoid duplication.
Step 1: Find Recent Posts tagged "Featured" and find the five posts that would otherwise be displayed as most recent
To begin, you'll need to set up two variables:featured_posts
and normal_recent_posts
The featured_post variable retrieves the post tagged as "Featured," and the normal_recent_posts variable retrieves the remaining posts for the listing.
{% set featured_posts = blog_recent_tag_posts("default", "featured", 1) %}
{% set normal_recent_posts = blog_recent_posts('default', 5) %}
Step 2: Identify the Featured Post ID
In this step, we'll extract the ID of the featured post from the featured_posts variable usingfeatured_posts[0].id. We'll need this to determine whether the featured post is included in the normal recent posts or not.
{% set featured_post_id = featured_posts[0].id %}
Step 3: Create an Array of IDs for Normal Recent Posts
To determine if the featured post is among the normal recent posts, we create an array of their IDs. Loop through the normal_recent_posts variable and use the post_array.append(post.id) code to add each post's ID to the array.
{% set post_array = [] %}
{% for post in normal_recent_posts %}
{% do post_array.append(post.id) %}
{% endfor %}
Step 4: Check if the Featured Post is Among Normal Recent Posts
We now check if the array of normal recent post IDs contains the ID of the featured post using the
HubSpot
Contact Forms
Custom Modules
hubl
hubspot
Wordpress
Ubermenu
Woocommerce
Wordpress
Other
Google Ads
Note To Self
Quickie