2

I want to show entries according to an array.

This is my code:

            {% set navsubids = craft.amNav.getNavRaw("hoofdnavigatie", {startFromId:25})  %}
            {% set navsubidarray = [] %}

            {% for navsubid in navsubids %}
                    {% set navsubidarray = navsubidarray|merge([navsubid.elementId]) %}
            {% endfor %}

            {% set navsubidarray = navsubidarray|join(', ') %}

            {{ navsubidarray }}<br>

            {% for entry in craft.entries.id('{{ navsubidarray }}') %}
                {{ entry.title }}<br>
            {% endfor %}

The variable gives back that what I need (18, 17, 19, 4, 20, 21, 22, 23) but the for entry loop isn't returning anything.

2 Answers 2

3

I'm not sure where you're getting the id's from but you will need to pass an array into craft.entries.id so try:

{% set navsubids = craft.amNav.getNavRaw("hoofdnavigatie", {startFromId:25})  %}

{% set navsubidarray = [] %}

{% for navsubid in navsubids %}
  {% set navsubidarray = navsubidarray|merge([navsubid.elementId]) %}
{% endfor %}

{% for entry in craft.entries.id( navsubidarray ) %}
  {{ entry.title }}<br>
{% endfor %}

When you used {% set navsubidarray = navsubidarray|join(', ') %} you were creating a string of the values in the array.

3

Luke's answer above is correct, you might as well just use the array directly.

But technically you can use a joined string as well, it's just your syntax is wrong - you just call the variable directly:

{% for entry in craft.entries.id(navsubidarray) %}
      {{ entry.title }}<br>
{% endfor %}

I just add this here in case it's ever useful to someone who for whatever reason wants to use a string of IDs in another scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.