AMP
Menggalangkebaikan.com
Fast mobile article powered by Nexiamath-SEO AMP.
AMP Article

Auto FAQ Scheme

Published · Updated · By Gita

add_filter( \'rank_math/json_ld\', function( $data, $jsonld ) {

if ( ! is_single() ) {
return $data;
}

global $post;
$content = $post->post_content;

// Deteksi section FAQ
if ( stripos( $content, \'

FAQ\' ) === false ) {
return $data;
}

preg_match_all(
\'/Q:\\s*(.*?)\\s*A:\\s*(.*?)(?=Q:|$)/s\',
wp_strip_all_tags( $content ),
$matches
);

if ( empty( $matches[1] ) ) {
return $data;
}

$faq = [
\'@type\' => \'FAQPage\',
\'mainEntity\' => []
];

foreach ( $matches[1] as $index => $question ) {
$faq[\'mainEntity\'][] = [
\'@type\' => \'Question\',
\'name\' => trim( $question ),
\'acceptedAnswer\' => [
\'@type\' => \'Answer\',
\'text\' => trim( $matches[2][ $index ] )
]
];
}

$data[\'faq_auto\'] = $faq;
return $data;

}, 99, 2 );