FAQPage structured data for accordion in Gutenberg block template
I´d like to add FAQPage schema in my Gutenberg block, that contais a accordion with questions and anwers.
I´m also using ACF to add the questions and answers repeater into the block.
I´v found a code with a model of what would suit me just fine, but the schema does not work as espected (only this line apears – <script type="application/ld+json">{"mainEntity":null}</script>
).
Other problem i´m facing is that as I insert the block into my editing page, it gets publishing error.
this is my edited code for Gutenberg Block with Faq schema:
<section class="faq accordion-1 section">
<div class="row">
<?php if (have_rows('faq')) : ?>
<?php while (have_rows('faq')) : the_row(); ?>
<h3 class="section-title"><?php the_sub_field('faq_section_name'); ?> (<?php echo count(get_sub_field('faq_section')); ?>)</h3>
<?php if (have_rows('faq_section')): ?>
<div class="accordion faq-questions" data-accordion data-allow-all-closed="true">
<?php while (have_rows('faq_section')): the_row(); ?>
<div class="question" data-accordion-item>
<header class="question-header">
<p class="question-title accordion-title"><?php the_sub_field('faq_question'); ?></p>
<span class="question-header-toggle"></span>
</header>
<div class="question-content" data-tab-content>
<?php the_sub_field('faq_answer'); ?>
</div>
</div>
<?php endwhile; ?>
</div><!-- end accordion faq-questions -->
<?php endif; ?>
<?php endwhile; ?>
<?php
$schema = array(
'@context' => "https://schema.org",
'Newtype' => "FAQPage",
'mainEntity' => array()
);
global $schema;
if ( have_rows('faq') ) {
while ( have_rows('faq') ) : the_row();
if ( have_rows('faq_section') ) {
while ( have_rows('faq_section') ) : the_row();
$questions = array(
'Newtype' => 'Question',
'name' => get_sub_field('faq_question'),
'acceptedAnswer' => array(
'Newtype' => "Answer",
'text' => get_sub_field('faq_answer')
)
);
array_push($schema['mainEntity'], $questions);
endwhile;
}
endwhile;
function fnz_generate_faq_schema ($schema) {
global $schema;
echo '<!-- Auto generated FAQ Structured data --><script type="application/ld+json">'. json_encode($schema) .'</script>';
}
add_action( 'wp_footer', 'fnz_generate_faq_schema', 100 );
}
?>
<?php endif; ?> <!-- endif have_rows('faq'); -->
</div>
</section>
Leave an answer
You must login or register to add a new answer .