Short version:
I want to add 1 to a number in a liquid template and use the result as an array index.
{% capture plus_one %}{{ 0 | plus: 1 }}{% endcaptur
{% for category in site.categories %}
{% assign catg_name = category.first %}
{% if catg_name == page.category %}
{% assign catg_posts = category.last %}
{% endif %}
{% endfor %}
{% for post in catg_posts %}
{% if post.title == page.title %}
{% unless forloop.last %}
{% assign next = catg_posts[forloop.index] %}
-
←{{ next.title }}
{% endunless %}
{% unless forloop.first %}
-
{{ prev.title }}→
{% endunless %}
{% endif %}
{% assign prev = post %}
{% endfor %}
As you have mentioned you can save and use the previous iteration value for the previous post link( in my case I use it as the next post link since I don't want the default newest-first order ). For the next array element you can use forloop.index
. This is the 1-based index of the for loop and will give you the next item of a zero-based array.