Home › Forums › Add-ons › Repeater Field › repeater field with schema.org FAQPage
Hello,
i have a question about the repeater field in conjunction with schema.org FAQPage.
I have a repeater field with two subfields (question & answer).
These fields should be output within a JSON script according to schema.org standard for FAQPage.
The output works so far. Each entry is separated by a comma. But no comma should appear after the last entry. What do I have to change in my code?
Here is my code example:
<?php if ( get_field( 'faq' ) ) : ?>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
<?php if ( have_rows( 'faq' ) ) : ?>
<?php while ( have_rows( 'faq' ) ) : the_row(); ?>
{
"@type": "Question",
"name": "<?php the_sub_field( 'question' ); ?>",
"acceptedAnswer": {
"@type": "Answer",
"text": "<?php the_sub_field( 'answer' ); ?>
}
},
<?php endwhile; ?>
<?php else : ?>
<?php // no rows found ?>
<?php endif; ?>
]
}
</script>
<?php endif; ?>
And the output that follows:
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "Question 1?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Answer 1"
}
},
{
"@type": "Question",
"name": "Question 2?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Answer 2"
}
},
{
"@type": "Question",
"name": "Question 3?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Answer 3"
}
},
]
}
</script>
Thanks for your help!
Michael
Your choices are
1) use a output buffer and then trim off the extra comma before output
ob_start();
// your loop that outputs content
$code = ob_get_clean();
$code = trim($code, ',');
2) Use a counter
// get # of rows
$rows = intval(get_field('repeater'));
$count = 1;
while(have_rows('repeater')) {
// output row
if ($count < $rows) {
// add a comma if there are more rows
?>,<?php
}
$count++;
}
Hello John,
thank you for your prompt assistance.
I’ve tried both variants several times now, but it won’t work.
Can you please show me how to use both options with my code?
Thanks a lot!
Michael
<?php
$rows = intval(get_field('faq'));
if (have_rows('faq')) {
$count = 1;
?>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
<?php
while (have_rows('faq')) {
the_row();
?>
{
"@type": "Question",
"name": "<?php the_sub_field( 'question' ); ?>",
"acceptedAnswer": {
"@type": "Answer",
"text": "<?php the_sub_field( 'answer' ); ?>
}
<?php
if ($count < $rows) {
?>,<?php
}
$count++;
}
?>
]
}
</script>
<?php
}
Hello John,
thanks for your help. Your code is still giving me a bug. At the end the closing ?>
is missing. I added it, then the output will be done. But now all entries are output without commas and not only the last one.
Unfortunately, it still doesn’t work. The commas are not output.
Here’s my complete code again:
<?php
$rows = intval(get_field('faq'));
if (have_rows('faq')) {
$count = 1;
?>
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
<?php
while (have_rows('faq')) {
the_row();
?>
{
"@type": "Question",
"name": "<?php the_sub_field( 'question' ); ?>",
"acceptedAnswer": {
"@type": "Answer",
"text": "<?php the_sub_field( 'answer' ); ?>
}
<?php
if ($count < $rows) {
?>,<?php
}
$count++;
}
?>
]
}
</script>
<?php
}
?>
<?php if( have_rows(‘faqs’) ): ?>
<?php $faqSchema = ”;
while( have_rows(‘faqs’) ): the_row();
$faqSchema .= ‘{
“@type”: “Question”,
“name”: “‘.get_sub_field(‘heading’).'”,
“acceptedAnswer”: {
“@type”: “Answer”,
“text”: “‘.get_sub_field(‘content’).'”
} },’;
endwhile;
?>
<script type=”application/ld+json”>
{
“@context”: “https://schema.org”,
“@type”: “FAQPage”,
“mainEntity”:[
<?php echo rtrim($faqSchema,’,’); ?>
]
}
<?php endif; ?>
</script>
The topic ‘repeater field with schema.org FAQPage’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.