Double post display

<div class="top-blog" style="background-image: url('https://f.hubspotusercontent20.net/hubfs/5974320/Blog/Groupe%20124.png')">
 <h1 class="page-center content-wrapper">
 Bienvenue sur le blog de Knauf Insulation
 </h1>
</div>

{% if not topic %}
<div class="page-center content-wrapper">
 {# TOP POST // ARTICLE A LA UNE #}
 {% set posts = blog_recent_posts('default', 1) %}
 {% for post in posts %}
 <div class="bl-top-post">
 <div class="bl-top-post-inner">
 <div class="bl-top-post-img">
 <a href="{{ content.absolute_url }}"><img src="{{ resize_image_url( post.post_list_summary_featured_image,690 ) }}" class="hs-featured-image"></a>
 </div>
 <div class="bl-top-post-content">
 {% if post.topic_list %}
 <div class="bl-top-content-cat">
 {% for tag in post.tag_list %} {# {% set t = t + 1 %} {% if t == 1 %} #}
 <a class="topic-link" href="{{ blog_tag_url(group.id, tag.slug) }}">{{ tag.name }}</a> 
 {# {% endif %} #} {% endfor %}
 <p><date>{{ post.publish_date_localized }}</date></p>
 </div>
 {% endif %}
 <h2><a href="{{post.absolute_url}}">{{ post.name }}</a></h2>
 <div class="bl-top-post-description">
 {{ post.post_list_content|safe|truncatehtml(200, ' ' ) }}
 </div>
 <div class="post-description-btn">
 <a class="more-btn" href="{{ post.absolute_url }}">Lire l'article </a>
 </div>
 </div>
 </div>
 </div>
 {% endfor %}
{% endif %}

 <div class="blog-section container">
 <div class="blog-listing-wrapper cell-wrapper">
 <div class="blog-section">
 <div class="blog-listing-wrapper cell-wrapper">
 <div class="">
 {% if topic %}
 <div class="topic-title">
 <h2>Articles : <span>{{ topic|replace('-',' ') }}</span></h2>
 </div>
 {% endif %} 
 {# simple_list_page indicates the "blog/all" page, which is a list of links to every blog post #}
 <div class="post-listing">
 {% for content in contents %}
 <div class="post-item">
 <div class="post-inner"> 
 <div class="hs-featured-image-wrapper" style="background: url({{ resize_image_url( content.post_list_summary_featured_image,400 ) }}) no-repeat; background-size:cover; ">
 {% for tag in content.tag_list %} {% set t = t + 1 %} {% if t == 1 %}
 <a class="topic-link tag-link" href="{{ blog_tag_url(group.id, tag.slug) }}">{{ tag.name }}</a> {% endif %} {% endfor %}
 </div> 

 <div class="post-header">
 <div class="date">
 <date>{{ content.publish_date_localized }}</date>
 </div>
 <h2><a href="{{content.absolute_url}}">{{ content.name }}</a></h2>
 <p>{{ content.post_summary|striptags|truncate(120, breakword=False, end='...') }}</p>
 <div class="post-description-btn">
 <a class="more-btn" href="{{ content.absolute_url }}">Lire l'article </a>
 </div>
 </div>
 </div>
 </div>

 {% if current_page_num > 1 %}
 <div class="post-item"> 
 <div class="post-inner">
 <div class="hs-featured-image-wrapper" style="background: url({{ resize_image_url( content.post_list_summary_featured_image,400 ) }}) no-repeat; background-size:cover; ">
 {% for tag in content.tag_list %} {% set t = t + 1 %} {% if t == 1 %}
 <a class="topic-link tag-link" href="{{ blog_tag_url(group.id, tag.slug) }}">{{ tag.name }}</a> {% endif %} {% endfor %}
 </div> 
 <div class="post-header">
 <div class="date">
 <date>{{ content.publish_date_localized }}</date> 
 </div>
 <h2><a href="{{content.absolute_url}}">{{ content.name }}</a></h2>
 <p>{{ content.post_summary|striptags|truncate(120, breakword=False, end='...') }}</p>
 <div class="post-description-btn">
 <a class="more-btn" href="{{ content.absolute_url }}">Lire l'article </a>
 </div>
 </div>
 </div>
 </div>
 {% endif %}
 {% if simple_list_page %}
 {% if loop.first %}
 <h2><br></h2>
 {% endif %}
 <h2 class="post-listing-simple"><a href="{{content.absolute_url}}">{{ content.name }}</a></h2>
 {% endif %}
 {% endfor %}

 </div>
 </div>

 {% if not simple_list_page %}
 <div class="blog-pagination">
 {% set page_list = [-2, -1, 0, 1, 2] %}
 {% if contents.total_page_count - current_page_num == 1 %}{% set offset = -1 %}
 {% elif contents.total_page_count - current_page_num == 0 %}{% set offset = -2 %}
 {% elif current_page_num == 2 %}{% set offset = 1 %}
 {% elif current_page_num == 1 %}{% set offset = 2 %}
 {% else %}{% set offset = 0 %}{% endif %}
 {% if last_page_num %}
 <a class="prev-link" href="{{ blog_page_link(last_page_num) }}">Précédente</a>
 {% endif %}
 {% for page in page_list %}
 {% set this_page = current_page_num + page + offset %}
 {% if this_page > 0 and this_page <= contents.total_page_count %}
 <a {% if this_page == current_page_num %}class="active"{% endif %} href="{{ blog_page_link(this_page) }}">{{ this_page }}</a>
 {% endif %}
 {% endfor %}
 {% if next_page_num %}<a class="next-link" href="{{ blog_page_link(current_page_num + 1) }}">Suivante</a>{% endif %}
 </div>
 {% endif %}

 </div>
 </div>
 </div>
 </div>
</div>

Thanks you :slightly_smiling_face:

Hi,

I am in the process of creating a blog, only I don’t understand, the last post is also found in the listing blog. How can O make sure that the first post is only displayed once ?


This is my blog : https://app.hubspot.com/design-previewer/5974320/templates/31421512165

And my code :

Hey @designfrance

I moved your post to the CMS board where you will get more responses :grinning_face:

@piersg is a talented fellow who could probably help here!

Hi @designfrance (thanks @dennisedson). Testing for the loop index and current page number should work.

The logic here is that your ‘top post’ is your most recent blog, so in the content in contents for loop we want to not display the first item (i.e. if loop.index > 1) and only do it on the first page of the blog (current_page_num == 1). You only need to do it on the first page because that should be the only page where the most recent blog is shown twice, as the top post and at the top of the ‘for content in contents’ list. Then on every other page display all blogs (or current_page_num > 1). I’m not sure why you had current_page_num > 1 in your original code, I can’t see the difference in the code there, so I’ve simplified that too.

{% for content in contents %}
 {% if (current_page_num == 1 and loop.index > 1) or current_page_num > 1 %}
 <div class="post-item">
 <div class="post-inner"> 
 <div class="hs-featured-image-wrapper" style="background: url({{ resize_image_url( content.post_list_summary_featured_image,400 ) }}) no-repeat; background-size:cover; ">
 {% for tag in content.tag_list %} {% set t = t + 1 %} {% if t == 1 %}
 <a class="topic-link tag-link" href="{{ blog_tag_url(group.id, tag.slug) }}">{{ tag.name }}</a> {% endif %} {% endfor %}
 </div> 

 <div class="post-header">
 <div class="date">
 <date>{{ content.publish_date_localized }}</date>
 </div>
 <h2><a href="{{content.absolute_url}}">{{ content.name }}</a></h2>
 <p>{{ content.post_summary|striptags|truncate(120, breakword=False, end='...') }}</p>
 <div class="post-description-btn">
 <a class="more-btn" href="{{ content.absolute_url }}">Lire l'article </a>
 </div>
 </div>
 </div>
 </div>
 {% endif %}
 {% if simple_list_page %}
 {% if loop.first %}
 <h2><br></h2>
 {% endif %}
 <h2 class="post-listing-simple"><a href="{{content.absolute_url}}">{{ content.name }}</a></h2>
 {% endif %}
{% endfor %}

Thank for your help @piersg ,

When I try this, all my articles disappear…

Try it like this {% if loop.index > 1 %}

@designfrance @dennisedson I would suggest you skip the first post from the listing… doing something like this:

{% for post in posts %}
{% if loop.index is not 1 %}
... 
{% endif %}
{% endfor %}