1

I'm trying to build an accordion in my matrix field (that also holds entries for a slideshow, button, image, etc).

Im having trouble with getting each entry to go inside < div id="accordion">< /div> and not repeat this accordion div over and over.

{% elseif block.type == "sectionaccordion" %}

    <div id="accordion">
      {% for block in entry.sectionaccordion %}
        <div class="accordiontitle">{{ block.accordionTitle }}</div>
        <div class="accordion-content default">
          {{ block.accordionText }}
        </div>
      {% endfor %}
    </div>

With this setup, I keep getting Craft\EntryModel and its behaviors do not have a method or closure named "sectionaccordion".

enter image description here

2
  • Is entry.sectionaccordion another matrix field or a block inside another matrix? You can't do matrix within a matrix inside Craft... Commented Oct 13, 2017 at 19:28
  • Nope its just a block type in my matrix. Updated with an image of my matrix settings.
    – kingkelly
    Commented Oct 13, 2017 at 19:49

2 Answers 2

3

The error has to do with the line {% for block in entry.sectionaccordion %} because it's looking for a field in your entry called sectionaccordion but sectionaccordion is not a field; it's a block inside a field (your matrix field). So the error is basically saying "there is no field called 'sectionaccordion' for this entry".

You also don't want to use a for loop here because there's nothing to loop through. You're already looping through matrix blocks created in this matrix field, and your code is saying "if the block is of type sectionaccordion, print the contents of it." The contents of that block won't be another loop; it'll only be 1 Accordion Title and 1 Accordion Text field.

Lastly, by saying {% elseif block.type == "sectionaccordion" %}, you are eliminating the chance to display a slideshow since that is a different block type, one not called sectionaccordion. If you want a slideshow inside your accordion block type, then you need to create a 3rd field for it inside sectionaccordion and just recreate whatever you did in the sectionslideshow block type.

3
  • Thanks, I think i realized what I was trying to do wont work with your second paragraph, since it would just be 1 accordian title and field. So how would someone compile a UL list using a matrix field? Thats basically what im trying to do.
    – kingkelly
    Commented Oct 13, 2017 at 21:02
  • You'd probably need to just create a matrix field for this called accordion or whatever. In that Matrix field, you'd only have one block type, and in that type you'd have the title and text fields.
    – Ryan
    Commented Oct 14, 2017 at 4:01
  • But thats not an option if you have a matrix field for filling up content on a page, and allowing the client to insert photos, text, buttons, captions etc. I want the accordian, or even a simple list, to be one of those options in the matrix field, so they can put it an any spot in their page.
    – kingkelly
    Commented Oct 15, 2017 at 4:54
1

Add a new option in your page matrix (the one with lots of options for photos, text etc) - an entries field that links to a new section called 'faqs'.

That 'faqs' section has one matrix in it - 'faqs', with two fields: 'faqTitle' and 'faqAnswer'.

The code would be something like: (you'd need to wrap it in your faq code, and assuming the entries field is in a matrix)

{% set relatedEntries = craft.entries.section('faqs').relatedTo(block) %}
{% for rel in relatedEntries %}
{% set relBlocks = rel.faqs.all() %}
{% for relBlock in relBlocks %}
{{ relBlock.faqTitle }}
{{ relBlock.faqAnswer }}
{% endfor %}
{% endfor %}

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.