I'm using Jekyll v4.2.0 / Liquid 4.X and would like to create page-based loop which returns related pages based on front matter tags, but that excludes the current page.
My page front matter markdown looks like this:
---
layout: test
title: Page about soda and chesse
permalink: /page-about-soda-and-cheese/
tags: soda, chesse
---
First I split the tags into an array, loop the pages and then use a if-statement to check if up to three tags matches the tag-string on other pages.
{% assign page-tags = page.tags | split: "," %}
<ul>
{% for page in site.pages %}
{% if page.tags contains page-tags[0] or page.tags contains page-tags[1] or page.tags contains page-tags[2] %}
<li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endif %}
{% endfor %}
</ul>
This correctly returns the following pages on my site:
/page-about-soda-and-cheese/
/page-about-cheese/
/page-about-soda/
However, I'd like to exclude the current page for the loop.
I have tried solving this with unless
-tag, but this returns nothing for some reason.
<ul>
{% for page in site.pages %}
{% if page.tags contains page-tags[0] or page.tags contains page-tags[1] or page.tags contains page-tags[2] %}
{% unless page.url == page.url %}
<li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endunless %}
{% endif %}
{% endfor %}
</ul>
Update: Also tried where_exp
based on this question Exclude current post from recent posts query in Jekyll?, but this also don't return anything.
<ul>
{% assign pages = site.pages | where_exp:"page","page.url != page.url" %}
{% for page in pages %}
{% if page.tags contains page-tags[0] or page.tags contains page-tags[1] or page.tags contains page-tags[2] %}
<li><a href="{{ page.url }}">{{ page.title }}</a></li>
{% endif %}
{% endfor %}
</ul>
Solved this myself. Changed for page
to for related
.
{% assign page-tags = page.tags | split: "," %}
<ul>
{% for related in site.pages %}
{% if related.tags contains page-tags[0] or related.tags contains page-tags[1] or related.tags contains page-tags[2] %}
{% if related.url != page.url %}
<li><a href="{{ related.url }}">{{ related.title }}</a></li>
{% endif %}
{% endif %}
{% endfor %}
</ul>