Support

Account

Home Forums Add-ons Repeater Field repeater field with schema.org FAQPage

Solving

repeater field with schema.org FAQPage

    • mvt78

    • June 11, 2020 at 10:25 pm

    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++;
    }
    
    • mvt78

    • June 12, 2020 at 4:54 am

    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 
      }
    
    • mvt78

    • June 12, 2020 at 9:46 pm

    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.

  • I had an extra } in the code, I corrected it above

    • mvt78

    • June 12, 2020 at 10:07 pm

    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 
    						  }
    						 ?>
Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.