Sergey Pervushin
Sergey Pervushin

Reputation: 363

Schema JSON и foreach php

I'm trying to make schema micro markup my FAQPage. My questions and answers are typed in the admin panel in the ACF repeater field and displayed on the site using foreach. I thought I could also do it to generate a json schema, but I can't implement it in any way, I'm still learning. Please tell me what's wrong with me

<?php
foreach ($faqs as $faq) {
    $faq .= '[';
    $faq .= '"@type" => "Question",';
    $faq .= '"name => "' . $faq['question'] . '",';
    $faq .= '"acceptedAnswer" => [';
    $faq .= '"@type => Answer",';
    $faq .= '"text => "' . $faq['answer'] . '",';
    $faq .= '],';
    $faq .= ']';
}

$data = [
    '@context' => '//schema.org',
    '@type' => 'FAQPage',
    'mainEntity' => [
        print_r($faq)
    ],
];

$data = json_encode($data);

echo '<script type="application/ld+json">' . $data . '</script>';
?>

$faq['question'] and $faq['answer'] contain a question and answer.

The result I expect:

<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"
    }
  }]
}
</script>

Upvotes: 0

Views: 300

Answers (1)

Jacob Mulquin
Jacob Mulquin

Reputation: 3608

You don't need to manually build JSON, you can use json_encode():

$faqs = [
    ['question' => 'What is the meaning of life?', 'answer' => '42'],
    ['question' => 'What is better, Star Wars or Star Trek?', 'answer' => 'Unable to answer'],
    ['question' => 'What is 1+1', 'answer' => '2, 11, window']
];

function schema_faq($faq)
{
    return [
        '@type' => 'Question',
        'name' => $faq['question'],
        'acceptedAnswer' => [
            '@type' => 'Answer',
            'text' => $faq['answer']
        ]
    ];
}

$schema_faqs = [];

foreach ($faqs as $faq) {
    $schema_faqs[] = schema_faq($faq);
}


$data = [
    '@context' => '//schema.org',
    '@type' => 'FAQPage',
    'mainEntity' => [
        $schema_faqs
    ],
];

$data = json_encode($data);

echo '<script type="application/ld+json">' . $data . '</script>';

Outputs:

<script type="application/ld+json">{
    "@context": "\/\/schema.org",
    "@type": "FAQPage",
    "mainEntity": [
        [
            {
                "@type": "Question",
                "name": "What is the meaning of life?",
                "acceptedAnswer": {
                    "@type": "Answer",
                    "text": "42"
                }
            },
            {
                "@type": "Question",
                "name": "What is better, Star Wars or Star Trek?",
                "acceptedAnswer": {
                    "@type": "Answer",
                    "text": "Unable to answer"
                }
            },
            {
                "@type": "Question",
                "name": "What is 1+1",
                "acceptedAnswer": {
                    "@type": "Answer",
                    "text": "2, 11, window"
                }
            }
        ]
    ]
}</script>

Upvotes: 1

Related Questions