Saturday, 18 January 2020

How to add structured data schema in array

How do i add structured data mainEntity as an array @type question, as you can see from my code i need to have main entity as an array so i can pass advanced custom fields repeater rows into schema in an attempt to build an faq

<?php 

$schema = array(
'@context'   => "https://schema.org",
'@type'      => "FAQPage",
'mainEntity' => array()
);

global $schema;


if ( have_rows('faq') ) {

    while ( have_rows('faq') ) : the_row();

        $questions = array(
            '@type'          => 'Question',
            'name'           => get_sub_field('question'),
            'acceptedAnswer' => array(
            '@type' => "Answer",
            'text' => get_sub_field('answer')
            )
            );
            array_push($schema['mainEntity'], $questions);
    endwhile;


function generate_faq_schema ($schema) {

  global $schema;

  echo '<script type="application/ld+json">'. json_encode($schema) .'</script>';

}

add_action( 'wp_footer', 'generate_faq_schema', 100 );


}

?>

<?php endif; ?> <!-- endif have_rows('faq'); -->


from How to add structured data schema in array

No comments:

Post a Comment