CMS Development

GWilkinson
Member

Blog Recent Posts + Featured Post on Top

SOLVE

Hello,

 

I am trying to display a blog listing where the post tagged "Featured" is always listed first and then the rest of the posts are underneath.

 

what I want to do is something like this:

 

{% set featured_posts = blog_tags('default', 'Featured', 1) %}
{% set recent_posts_limit = 5 %}
{% set excluded_post_id = 0 %}

{% if featured_posts.posts|length %}
  {% set excluded_post_id = featured_posts.posts[0].id %}
  {% set recent_posts = blog_recent_posts('default', recent_posts_limit - 1, exclude_ids=excluded_post_id) %}
{% else %}
  {% set recent_posts = blog_recent_posts('default', recent_posts_limit) %}
{% endif %}

<div class="recent-posts">
  {% if featured_posts.posts|length %}
    {% for post in featured_posts.posts %}
      <div class="featured-post">
        <h3><a href="{{ post.absolute_url }}">{{ post.name }}</a></h3>
        <p>{{ post.post_summary }}</p>
        <span>Posted on {{ post.publish_date|datetimeformat('%Y-%m-%d') }}</span>
      </div>
    {% endfor %}
  {% endif %}

  {% for post in recent_posts.posts %}
    <div class="recent-post">
      <h3><a href="{{ post.absolute_url }}">{{ post.name }}</a></h3>
    </div>
  {% endfor %}

 

But there is no exclude_ids. Ive seen a few examples with two loops but im not sure how to explicitly exclude a tag. I tried topics but am not using a module for this so I dont think that was working for me either.

0 Upvotes
1 Accepted solution
BarryGrennan
Solution
Top Contributor

Blog Recent Posts + Featured Post on Top

SOLVE

This should work:

{% set featured_posts = blog_recent_tag_posts("default", "featured", 1) %} {#featured is lowercase here as it's the slug #} {# also note we're using blog_recent_tag_posts to get the posts not blog_tags #}

{% set normal_recent_posts = blog_recent_posts('default', 5) %}

{% set featured_post_id = featured_posts[0].id %}

{# we want to work out if the featured post would show in the normal 5 recent posts, so we create an array of their ids #}
{% set post_array = [] %}
 {% for post in normal_recent_posts %}
      {% do post_array.append(post.id) %}
{% endfor %}

{# if the featured post is in the standard 5 we're going to add an extra post (6 total) so we can hide the featured in the loop #}
{% if post_array is containing featured_post_id %}
{% set displayed_recent_posts = blog_recent_posts('default', 6) %}
{% else %}
{% set displayed_recent_posts = blog_recent_posts('default', 5) %}
{% endif %}

FEATURED POSTS

 {% for post in featured_posts %}
 <div class="recent-post">
      <h3><a href="{{ post.absolute_url }}">{{ post.name }}</a></h3>
    </div>
  {% endfor %}

NORMAL POSTS
  {% for post in displayed_recent_posts %}
{% unless post.id == featured_post_id %}
    <div class="recent-post">
      <h3><a href="{{ post.absolute_url }}">{{ post.name }}</a></h3>
    </div>
{% endunless %}
  {% endfor %}

profile2022aBarry Grennan

Freelance HubSpot CMS Developer

Website | Contact | LinkedIn

 

 

View solution in original post

0 Upvotes
4 Replies 4
BarryGrennan
Solution
Top Contributor

Blog Recent Posts + Featured Post on Top

SOLVE

This should work:

{% set featured_posts = blog_recent_tag_posts("default", "featured", 1) %} {#featured is lowercase here as it's the slug #} {# also note we're using blog_recent_tag_posts to get the posts not blog_tags #}

{% set normal_recent_posts = blog_recent_posts('default', 5) %}

{% set featured_post_id = featured_posts[0].id %}

{# we want to work out if the featured post would show in the normal 5 recent posts, so we create an array of their ids #}
{% set post_array = [] %}
 {% for post in normal_recent_posts %}
      {% do post_array.append(post.id) %}
{% endfor %}

{# if the featured post is in the standard 5 we're going to add an extra post (6 total) so we can hide the featured in the loop #}
{% if post_array is containing featured_post_id %}
{% set displayed_recent_posts = blog_recent_posts('default', 6) %}
{% else %}
{% set displayed_recent_posts = blog_recent_posts('default', 5) %}
{% endif %}

FEATURED POSTS

 {% for post in featured_posts %}
 <div class="recent-post">
      <h3><a href="{{ post.absolute_url }}">{{ post.name }}</a></h3>
    </div>
  {% endfor %}

NORMAL POSTS
  {% for post in displayed_recent_posts %}
{% unless post.id == featured_post_id %}
    <div class="recent-post">
      <h3><a href="{{ post.absolute_url }}">{{ post.name }}</a></h3>
    </div>
{% endunless %}
  {% endfor %}

profile2022aBarry Grennan

Freelance HubSpot CMS Developer

Website | Contact | LinkedIn

 

 

0 Upvotes
GWilkinson
Member

Blog Recent Posts + Featured Post on Top

SOLVE

Thank you! Worked perfectly

0 Upvotes
Jaycee_Lewis
Community Manager
Community Manager

Blog Recent Posts + Featured Post on Top

SOLVE

Hey, @GWilkinson 👋 I moved this over to our CMS developer community. I'd also like to invite some of our community members to the conversation — @BarryGrennan @JBeatty, do you have any tips you can share with @GWilkinson?

 

Thank you very much for taking a look! — Jaycee

linkedin

Jaycee Lewis

Developer Community Manager

Community | HubSpot

0 Upvotes
GWilkinson
Member

Blog Recent Posts + Featured Post on Top

SOLVE

Thank you!

0 Upvotes