I have two arrays of entires.
One for featured articles and another for the rest.
{% set featuredArticles = entry.featuredArticles.article.all() %}
{% set articles = craft.entries().section('newsArticles').all() %}
The max amount of featured articles the page can have is 2 - but the user might only select one or none. So I have written a condition to sanitise the array so there are not duplications i.e.
If a article is a featured article, remove it from the articles list.
I've got parts of it working but was wondering if there was a better way to do this? Any suggestions would be great.
{# Get ARTICLES - featured and normal #}
{% set featuredArticles = entry.featuredArticles.article.all() %}
{% set articles = craft.entries().section('newsArticles').all() %}
{# Featured articles #}
{% if featuredArticles|length %}
<div class="featured_articles">
{#
*** TWO FEATURED ARTICLES ***
#}
{% if featuredArticles|length == 2 %}
{% for featuredArticle in featuredArticles %}
{% if featuredArticle is defined and featuredArticle is not empty %}
{% include "_includes/blocks/news_card" with { block: featuredArticle, featured: true } %}
{% endif %}
{# Sanatize featured articles from artciles array #}
{% set url = featuredArticle.url %}
{% for article in articles %}
{% if article.url == url %}
{% set articles = articles | slice(0,loop.index) %}
{% endif %}
{% endfor %}
{% endfor %}
{#
*** ONE FEATURED ARTICLES ***
#}
{% elseif featuredArticles|length == 1 %}
{% include "_includes/blocks/news_card" with { block: featuredArticles|first, featured: true } %}
{% set url = featuredArticles|first.url %}
{% for article in articles %}
{% if article.url == url %}
{% set articles = articles | slice(0,loop.index) %}
{% endif %}
{% endfor %}
{% include "_includes/blocks/news_card" with { block: articles|first, featured: true } %}
{% endif %}
</div>
{#
*** NO FEATURED ARTICLES ***
#}
{% else %}
<div class="featured_articles">
{% include "_includes/blocks/news_card" with { block: articles[0], featured: true } %}
{% include "_includes/blocks/news_card" with { block: articles[1], featured: true } %}
</div>
{# Sanatize articles array #}
{# [2:] omits the first two itterations of the array #}
{% set articles = articles[2:] %}
{% endif %}
Thank you for your amazing answer - Its really helped!
There was a small part that I had missed out about housing the featured articles in a div and the articles in a div for styling them.
I managed to write some if/else to do this adapting your code:
{# Get ARTICLES - featured and normal #}
{% set featuredArticles = entry.featuredArticles.article.all() %}
{% set articles = craft.entries()
.section('newsArticles')
.id(['not']|merge(featuredArticles|column('id')))
.all()
%}
{% set articles = featuredArticles|merge(articles) %}
{% for article in articles %}
{# Open featured article div on first loop #}
{% if loop.first %}
<div class="featured_articles">
{% endif %}
{# Set featured articles #}
{% if loop.index <= 2 %}
{{ include("_includes/blocks/news_card", {
block: article,
featured: loop.index <= 2
}) }}
{# Close featured Loop #}
{% if loop.index == 2 %}
</div>
{% endif %}
{% else %}
{# Open articles div #}
{% if loop.index == 3 %}
<div class="news_articles">
{% endif %}
{{ include("_includes/blocks/news_card", {
block: article,
featured: loop.index <= 2
}) }}
{% if loop.last %}
</div>
{% endif %}
{% endif %}
{% endfor %}
Again thank you so much for your help on this - there is alot to learn about what can be done in twig when it comes to manipulating data.