I’m having some trouble with my blog listings. I want to feature one specific blog post at the top of the page, followed by the rest of the blog posts in chronological order. I’ve created some test blog posts, including one that I’ve marked as featured for testing.
Currently, my blog page displays the latest posts, which is good, but I can’t figure out how to ensure that the featured post comes first. Additionally, I want to label the regular posts section with a heading like ‘Recent Articles’, but since I’m using a loop for displaying the posts, the heading ends up showing on every post card instead of just once.
Could someone take a look at my code and help with this issue? Here is what I have:
{% set featured_posts = blog_recent_topic_posts('default', 'featured', 1) %}
{% for post in featured_posts %}
{% for topic in post.topic_list %}
{% if topic.name == 'featured' %}
<section class="blog-index-list">
<article class="blog-index__post-wrapper-list-">
<div class="blog-index__post-list">
{% if post.featured_image and group.use_featured_image_in_summary %}
<a class="blog-index__post-image-list"
style="background-image: url('{{ post.featured_image }}');"
href="{{ post.absolute_url }}">
</a>
{% endif %}
<div class="blog-index__post-content-list">
<div>
{% set featured_tag = post.topic_list | first %}
{% if featured_tag %}
<span class="blog-index__post-preheader-list">{{ featured_tag }}</span>
{% endif %}
<div class="blog-index__post-meta-list">
<span class="blog-index__post-author-list">
{{ post.blog_post_author }} |
</span>
<span class="blog-index__post-date-list">
{{ post.publish_date | datetimeformat('%b %e, %Y') }}
</span>
</div>
<h3><div class="blog-title"><a href="{{ post.absolute_url }}">{{ post.name }}</a></div></h3>
{% if content_group.show_summary_in_listing %}
<div class="meta-description">{{ post.meta_description | default(post.post_summary, true) | truncatehtml(250, '...', false) }}</div>
{% endif %}
</div>
<a href="{{ post.absolute_url }}"> <button class="blog-button-cta"> Read More </button> </a>
</div>
</div>
</article>
</section>
{% endif %}
{% endfor %}
{% endfor %}
{# Listings for other posts #}
{% set remaining_posts = contents | sort(attribute='publish_date', reverse=True) %}
{% for content in remaining_posts %}
<article class="blog-index__post-wrapper-list">
<div class="blog-index__post-list">
{% if content.featured_image and group.use_featured_image_in_summary %}
<a class="blog-index__post-image-list"
style="background-image: url('{{ content.featured_image }}');"
href="{{ content.absolute_url }}">
</a>
{% endif %}
<div class="blog-index__post-content-list">
<div>
{% set featured_tag = content.topic_list | first %}
{% if featured_tag %}
<span class="blog-index__post-preheader-list">{{ featured_tag }}</span>
{% endif %}
<h3><div class="blog-title"><a href="{{ content.absolute_url }}">{{ content.name }}</a></div></h3>
<div class="blog-index__post-meta-list">
<span class="blog-index__post-author-list">
{{ content.blog_post_author }} |
</span>
<span class="blog-index__post-date-list">
{{ content.publish_date | datetimeformat('%b %e, %Y') }}
</span>
</div>
{% if content_group.show_summary_in_listing %}
<div class="meta-description">{{ content.meta_description | default(content.post_summary, true) | truncatehtml(250, '...', false) }}</div>
{% endif %}
</div>
<a href="{{ content.absolute_url }}"> <button class="blog-button-cta"> Read More </button> </a>
</div>
</div>
</article>
{% endfor %}