FAQ Structured Data Markup is something new to our digital life. Google now can display directly in the search results Frequently asked questions about a specific topic. In this article, you will learn how you can implement this new FAQ rich results with Advanced Custom Fields in your WordPress Theme. To follow this tutorial you should have FTP Access to your site and an admin account to install Advanced Custom Fields Pro.
Create FAQ Structured Data Markup from ACF fields
You have to know that the following example will work only inside the WordPress loop.
Analyze our FAQ Structured Data setup
Our website has an FAQ section and this will be our walk-through for this tutorial. This page was created by using ACF PRO and repeater fields. Our Fields setup Contains, where – is an inheritance indicator.
- A parent repeater named FAQ – Repeater Field
- A child field named FAQ Section Name – – Text Field
- A nested repeater named FAQ Section – – Repeater Field
- A child Text area field of nested repeater named Question – – – Text Area Field
- A child Wysiwyg Editor field of nested repeater named Answer – – – Wysiwyg Editor
So, our Advanced Custom Fields Field editor should look like:
Let’s code those fields
A new page template is the best idea to build our FAQ section. We strongly recommend to follow this way but you can also use other methods like page or post even custom post type but maybe some steps could be a little bit different. So the FAQ fields based on ACF should have this markup in PHP.
<?php if (have_rows('faq')) : ?>
<?php while (have_rows('faq')) : the_row(); ?>
<h3 class="playfair bold extra-margin"><?php the_sub_field('faq_section_name'); ?> (<?php echo count(get_sub_field('faq_section')); ?>)</h3>
<?php if (have_rows('faq_section')): ?>
<ul class="accordion" data-accordion data-allow-all-closed="true">
<?php while (have_rows('faq_section')): the_row(); ?>
<li class="" data-accordion-item>
<a href="#" class="accordion-title yantramanav bold"><?php the_sub_field('faq_question'); ?></a>
<div class="accordion-content yantramanav light" data-tab-content>
<?php the_sub_field('faq_answer'); ?>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
Create the FAQ Structured Data Markup
Once our main loop has run we need to create the same loop again in order to get the fields ready for JSON encoding. Notice that we do not want our FAQ Structured Data Markup to appear inside the elements. We want it to the footer of the site for many reasons, mainly because we are going to generate a script. This time our loop should look like:
<?php
global $schema;
$schema = array(
'@context' => "https://schema.org",
'@type' => "FAQPage",
'mainEntity' => array()
);
if ( have_rows('faq') ) {
while ( have_rows('faq') ) : the_row();
if ( have_rows('faq_section') ) {
while ( have_rows('faq_section') ) : the_row();
$questions = array(
'@type' => 'Question',
'name' => get_sub_field('faq_question'),
'acceptedAnswer' => array(
'@type' => "Answer",
'text' => get_sub_field('faq_answer')
)
);
array_push($schema['mainEntity'], $questions);
endwhile;
}
endwhile;
function bakemywp_generate_faq_schema ($schema) {
global $schema;
echo '<!-- Auto generated FAQ Structured data by Bakemywp.com --><script type="application/ld+json">'. json_encode($schema) .'</script>';
}
add_action( 'wp_footer', 'bakemywp_generate_faq_schema', 100 );
}
?>
Put it all together
At the end our FAQ Structured Data ACF loop looks awesome!
<?php if (have_rows('faq')) : ?>
<?php while (have_rows('faq')) : the_row(); ?>
<h3 class="playfair bold extra-margin"><?php the_sub_field('faq_section_name'); ?> (<?php echo count(get_sub_field('faq_section')); ?>)</h3>
<?php if (have_rows('faq_section')): ?>
<ul class="accordion" data-accordion data-allow-all-closed="true">
<?php while (have_rows('faq_section')): the_row(); ?>
<li class="" data-accordion-item>
<a href="#" class="accordion-title yantramanav bold"><?php the_sub_field('faq_question'); ?></a>
<div class="accordion-content yantramanav light" data-tab-content>
<?php the_sub_field('faq_answer'); ?>
</div>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php endwhile; ?>
<?php
global $schema;
$schema = array(
'@context' => "https://schema.org",
'@type' => "FAQPage",
'mainEntity' => array()
);
if ( have_rows('faq') ) {
while ( have_rows('faq') ) : the_row();
if ( have_rows('faq_section') ) {
while ( have_rows('faq_section') ) : the_row();
$questions = array(
'@type' => 'Question',
'name' => get_sub_field('faq_question'),
'acceptedAnswer' => array(
'@type' => "Answer",
'text' => get_sub_field('faq_answer')
)
);
array_push($schema['mainEntity'], $questions);
endwhile;
}
endwhile;
function bakemywp_generate_faq_schema ($schema) {
global $schema;
echo '<!-- Auto generated FAQ Structured data by Bakemywp.com --><script type="application/ld+json">'. json_encode($schema) .'</script>';
}
add_action( 'wp_footer', 'bakemywp_generate_faq_schema', 100 );
}
?>
<?php endif; ?> <!-- endif have_rows('faq'); -->
Testing our page on Google
If everything is working fine in Google Structured Data Testing Tool your site should have no errors in search results.
Hi,
this is exactly what I was needing, but…
the line 3
()
throws an error (PHP 7.3.):
PHP Warning: count(): Parameter must be an array or an object that implements Countable
What could it be and how to resolve it?
Thanks letting us know that this article helped you!
In line 3 to avoid the PHP warning you should do a check first to see if the count function will have some results. It seems that you do not have any data to display and the counter seems confused. Take a look here: https://stackoverflow.com/questions/51594817/php-7-2-warning-count-parameter-must-be-an-array-or-an-object-that-impleme
Never mind, I solved it. You can delete those two comments. ๐
Thx for this wonderful script. Very helpful in my case.
Oliver
Glad to listen that Oliver!
Thank you.
I’ve had issues using this. When I’ve check the source the code gets up to main entity but says $null and no other information is displayed. But the FAQ displays correct on the site?
Hey Ryan! If you getting this error probably you have made something wrong with the sub fields of the repeater. Can you please send us an email to hello[at]bakemywp.com the URL of the page you are seeing this error in order to assist you further? Thanks!
I copied it exactly from what I could see written and on your image
What do you think it could be?
Have you registered the same fields in ACF with exact the same names? For this article I have copied this code from our FAQ page https://bakemywp.com/faqs/ and as you can see is working fine. If you don’t want to share in public your site URL drop us an email to take a look.
I’ve emailed you.
Yes Ryan, check your inbox we have replied to you. Thanks!
Hi, do you have same example for ‘Recipe’ schema? ๐
Hi Eugen your comment is noted, we can do another post with this schema. We will publish it during the next 2 weeks. Thanks!
Great! Thank you!
I still wait guys ๐
Hello Eugen! We can not provide an estimate about this blog post because our schedule is overloaded due to COVID-19. Thanks for your patience.
Hi i need your help, i have a php faq page, i want to make this type please help me
To assist you further please submit our quote form and you will get a quick reply!
I am getting this as a result – followed the guide step by step
{“mainEntity”:null}
Any ideas?
Hey Kyle! Probably you are doing something wrong in this step array_push($schema[‘mainEntity’], $questions); If you need further help please contact us at hello[at]bakemywp.com
Not sure how emailing will help as I am your code exactly as it is on this page. Field names in ACF match as well as I get the front end showing ok its just the Schema that doesn’t work.
Emailing to us will help to find what causes the empty array. As you can see in this page we follow exact the same code and its working fine in Structured Data Testing Tool.
For all having this problem with {โmainEntityโ:null} – In the code shown in the tutorial global $schema has to be put BEFORE $schema array definition:
global $schema;
$schema = array(
‘@context’ => “https://schema.org”,
‘@type’ => “FAQPage”,
‘mainEntity’ => array()
);
Thanks a lot Gino! I have also updated the tutorial code.
Thank you for this however I still get the error “A value for the mainEntity field is required.”
My code:
global $schema;
$schema = array(
‘@context’ => “https://schema.org”,
‘@type’ => “FAQPage”,
‘mainEntity’ => array()
);
if (have_rows(‘section_accordion’)) {
while ( have_rows(‘section_accordion’) ) : the_row();
$questions = array(
‘@type’ => ‘Question’,
‘name’ => get_sub_field(‘section_accordion_item_title’),
‘acceptedAnswer’ => array(
‘@type’ => “Answer”,
‘text’ => get_sub_field(‘section_accordion_item_content’)
)
);
array_push($schema[‘mainEntity’], $questions);
endwhile;
}
function bakemywp_generate_faq_schema($schema) {
global $schema;
echo ‘‘ . json_encode($schema) . ”;
}
add_action(‘wp_footer’, ‘bakemywp_generate_faq_schema’, 100);
Hello, I been trying to convert this FAQ style to a Local Business scheme but I couldn’t get it to work. Could you please help me to achieve this.
Hello Maksut! This code is only for FAQ. It can not be converted to Local Business because Local Business is something different. We can do this for you as a task. In order to make a request please visit our Free Quote page and submit a new ticket there! Thanks!
Hi
Thank you for this template.
However, if I want to set this up inside an ACF custom block, how would that look?
Using the code above in a block returns the errors “Warning: array_push() expects parameter 1 to be array” and “cannot redeclare the function twice” (the function that outputs the Schema)
Thank you.
I want to hook this code into Header inside head tag, except for footer so what should be the code will?
i have applied this but it is not working
add_action( ‘wp_head’, ‘bakemywp_generate_faq_schema’, 100 );
thanks in advance