0

I'm having a bit of difficulty having 2 different matrix blocks show on one page, and they both have Entries fields. As soon as I put {% set entry for my first Matrix, the second Matrix disappears.

{% for block in entry.sideMenu %}
 {% if block.type == "sideMenuItem" %}
    {% set entry = block.sideLink.first() %}
      {% if entry %}
         <a href="{{ entry.url }}">{{ block.sideTitle }}</a>
      {% endif %}
 {% endif %}
{% endfor %}
1

If you look at how Craft handles routing you will see this:

If the URI matches an entry’s or category’s URI, the section’s/category group’s template will get loaded, and the matched element will be made available to the template via a pre-populated entry or category variable.

So Craft is assigning that entry variable that you are referencing on line 1 of your code. Then on line 3, you do {% set entry = block.sideLink.first() %} which overwrites that initial entry variable with this new value, which is why the second matrix disappears.

What you probably want to do is this:

{% for block in entry.sideMenu %}
  {% if block.type == "sideMenuItem" %}
    {% set sideLinkEntry = block.sideLink.first() %}
    {% if sideLinkEntry %}
       <a href="{{ sideLinkEntry.url }}">{{ block.sideTitle }}</a>
    {% endif %}
  {% endif %}
{% endfor %}

And your original entry will be unmodified.

2
  • Oh hells yeah that worked! Thanks so much, i can finally move on with my life!
    – kingkelly
    Oct 4 '17 at 19:30
  • Glad I could help! Hope I managed to make the whole thing a bit less mysterious Oct 4 '17 at 23:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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