Hi @charlie!
Your TWIG FAQ solution is amazing!
I tried modifying it so that the dynamic looper uses ACF relation fields instead of the the List group parameter.
Your dynamic content is {{dc:p:faq}}
, whereas mine is {{dc:acf:post_field field="service_faq_relations"}}
.
Your question is {{ looper.item.question }}
, whereas mine is {{dc:acf:post_field field="faq_question"}}
.
Your answer is {{ looper.item.answer }}
, whereas mine is {{dc:acf:post_field field="faq_answer"}}
.
(I tried {{acf.post_field field="faq_question"}}
, but that didn’t work, so I left the dc
and the columns as they are.)
This is your original TWIG:
Your Original TWIG
{# Setup questions array #}
{% set questions = [] %}
{% for item in p.faq %}
{% set questions = questions|push({
"@type": "Question",
"name": item.question,
"acceptedAnswer": {
"@type": "Answer",
"text": item.answer | striptags
}
}) %}
{% endfor %}
{# Setup and output Schema #}
{% set schema = {
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": questions
} %}
{# Debug {{ schema | json_encode }} #}
<script type="application/ld+json">
{{ schema | json_encode | raw }}
</script>
This is my updated TWIG, which reflects different dynamic content references and parameters:
My Updated TWIG
{# Setup questions array #}
{% set questions = [] %}
{% for item in looper.array %}
{% set questions = questions|push({
"@type": "Question",
"name": item.faq_question,
"acceptedAnswer": {
"@type": "Answer",
"text": item.faq_answer | striptags
}
}) %}
{% endfor %}
{# Setup and output Schema #}
{% set schema = {
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": questions
} %}
{# Debug #}
{# {{ schema | json_encode }} #}
<script type="application/ld+json">
{{ schema | json_encode | raw }}
</script>
I am not getting any immediate errors, but the front end is not rendering the schema. This is all I get:
<script type="application/ld+json">
{"@context":"https:\/\/schema.org","@type":"FAQPage","mainEntity":[]}
</script>
Followed by a few empty <div>
elements.
If this isn’t something that would take too much of your time, it would be amazing if you could provide a working TWIG snippet that generates an FAQ Schema from ACF dynamic content. I believe many of us are generating FAQs this way.
Thank you!