Hi,
my code:
<?php
$schema = array(
'@context' => "https://schema.org",
'@type' => "FAQPage",
'mainEntity' => array()
);
global $schema;
if (have_rows('pytania_i_odpowiedzi')) {
while (have_rows('pytania_i_odpowiedzi')) : the_row();
$questions = array(
'@type' => 'Question',
'name' => get_sub_field('pytanie'),
'acceptedAnswer' => array(
'@type' => "Answer",
'text' => get_sub_field('odpowiedz')
)
);
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);
}
?>
I am getting an error:
array_push() expects parameter 1 to be array, null given
How to fix it?
I have a solution
global $schema;
$schema = array(
'@context' => "https://schema.org",
'@type' => "FAQPage",
'mainEntity' => array()
);