Ответы пользователя по тегу WordPress
  • Как правильно использовать функцию php?

    condor-bird
    @condor-bird
    Завертываем код в функцию
    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>
        ';
    }


    Там где нужно, вызываем функцию и передаем любые параметры:

    echo generateFaqSchema(get_the_ID());
    Ответ написан