function generateFaqSchema($postId) {
$repeater = get_post_meta($postId, 'faqshema', true);
$count = count($repeater);
$jsonLd = [
"@context" => "https://schema.org",
"@type" => "FAQPage",
"mainEntity" => []
];
foreach ($repeater as $item) {
$question = $item['question'];
$answer = $item['answer'];
$jsonLd['mainEntity'][] = [
"@type" => "Question",
"name" => $question,
"acceptedAnswer" => [
"@type" => "Answer",
"text" => $answer
]
];
$count--;
if ($count > 0) {
$jsonLd['mainEntity'][] = ',';
}
}
return '
<script type="application/ld+json">
' . json_encode($jsonLd) . '
</script>
';
}