Hey there, how are you doing?
I’m trying to create a section to display related posts, but if there is no related posts, it should not print the H1 on screen. The H1 is out of the loop area and I’m having difficulty to don’t show it if the number of related is 0;
This is the code:
<section class="related-posts">
<div class="box-container">
{% if isHeaderDisplayed != false %}
<h1 class="related-title">
{% if site.lang == "en" %}
Related Posts
{% else %}
Posts Relacionados
{% endif %}
</h1>
{% endif %}
<ul class="related-wrap">
{% assign maxRelated = 3 %}
{% assign minCommonTags = 1 %}
{% assign maxRelatedCounter = 0 %}
{% for post in site.posts %}
{% assign sameTagCount = 0 %}
{% for category in post.categories %}
{% if post.url != page.url %}
{% if page.categories contains category %}
{% assign sameTagCount = sameTagCount | plus: 1 %}
{% endif %}
{% endif %}
{% endfor %}
{% if sameTagCount >= minCommonTags %}
<li class="related-card">
<a href="{{ site.baseurl }}{{ post.url }}">
<img src="{{ post.img_featured }}" width="330" height="257" class="related-image">
<div class="related-card-title-wrap">
<span class="related-post-category">
{% case post.categories.first %}
{% when 'seguranca' %}Segurança
{% else %}
{{ post.categories.first}}
{% endcase %}
</span>
<h2 class="related-post-title">{{ post.title }}</h2>
</div>
</a>
</li>
{% assign maxRelatedCounter=maxRelatedCounter | plus: 1 %}
{% if maxRelatedCounter>= maxRelated %}
{% break %}
{% endif %}
{% if maxRelatedCounter <= maxRelated %} {% assign isHeaderDisplayed=false%} {% endif %} {% endif %} {%
endfor %} </ul>
</div>
</section>
What I tried is to set a variable isHeaderDisplayed and if its true, the H1 would show, if not…
But it is not working