1

I have a Faq page with an accordion, and it works perfectly, yet I have two identical pages in the same faq folder with the same exact code but it only works on the index.twig and not on the other two pages. On all the pages I have under the Faq a contact form and on the other two pages there is an extra code for if an error occurs or a success. I did the same principle on my contact page also three pages and two with the success and error code in it.

It goes wrong with this code:

{% for Faqs in entry.faqs.all() %}
    <div class="card-header">
        <h4 class="card-title m-0">
            <a class="accordion-toggle" data-bs-toggle="collapse" data-bs-parent="#accordion{{count}}" href="#collapse{{count}}">
                <i class="fas fa-users"></i>
                {{Faqs.question}}
            </a>
        </h4>
    </div>
    <div id="collapse{{count}}" class="collapse show{{count}}">
        <div class="card-body">
            <p class="mb-0 text-5">{{Faqs.answers}}</p>
        </div>
        {% set count = count+1 %}
    </div>
{% endfor %}

I get an error Variable "entry" does not exist, yet the same code works on the index.twig and not on the other two.

And before you think what other code, it is just html underneath the form.

What am I doing wrong, and why does it work with the contact pages and not on the faq pages???

Brad Bell
  • 67,440
  • 6
  • 73
  • 143

1 Answers1

2

I get an error Variable "entry" does not exist.

This means that when you're trying to use entry.faqs.all(), the entry variable doesn't exist, so you get this error. entry.faqs accesses a field named faq of the current entry. So you will get different results on different pages.

If the entry variable doesn't exist, this usually means that the current route does not belong to an entry at all. It might be a category or a custom route, both of which don't set an entry variable by default.

If you want to display FAQ entries from a field on the current entry, make sure that you only include that code on templates that belong to entries with that field.

However, if you want to display the same FAQ entries from one specific entry, you need to use an entry query to find that entry. Then you can access the FAQ field from anywhere. For example:

{% set faq_entry = craft.entries()
    .section('my_faq_section')
    .one()
%}
{% set faqs = faq_entry.faqs.all() %}
MoritzLost
  • 11,215
  • 5
  • 22