Support

Account

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

Solving

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.

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

  • 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&#8221;,

    “@type”: “FAQPage”,
    “mainEntity”:[
    <?php echo rtrim($faqSchema,’,’); ?>
    ]
    }
    <?php endif; ?>
    </script>

  • Does anyone know why code is not working

Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘repeater field with schema.org FAQPage’ is closed to new replies.