skip to Main Content

I have this code in my liquid file:

{% assign beatles = article.content | split: "</p>" %}
{% for member in beatles %}
  {{ member }}
{% endfor %}

What I am trying to do is insert content every 2nd, 4th, 6th paragraph.

How do I do this, I understand I can get content from the array using {{ member[2] }} for example, but how do I then insert content after this paragraph?

2

Answers


  1. Chosen as BEST ANSWER

    Found out a way to do this. Within the for loop I needed to add

    {% if forloop.index == 2 %}
        <p>PARA 2</p>
    {% endif %}
    {% if forloop.index == 4 %}
        <p>PARA 4</p>
    {% endif %}
    

    etc. Hope that helps others.


  2. I hope this code helps:

    {%- assign modulo = forloop.index | modulo: 2 -%}
    {% if modulo == 0 and forloop.index <= 6 %}
      <p>PARA  {{ forloop.index }}</p>
    {%- endif -%}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search