get_faq( get_the_ID() ); if ( empty( $faq ) ) { return; } $entities = array(); foreach ( $faq as $item ) { $entities[] = array( '@type' => 'Question', 'name' => $item['question'], 'acceptedAnswer' => array( '@type' => 'Answer', 'text' => $item['answer'], ), ); } $schema = array( '@context' => 'https://schema.org', '@type' => 'FAQPage', 'mainEntity' => $entities, ); echo '' . "\n"; } /** * Append a visible FAQ section to the post content. * * @param string $content Post content. * @return string */ public function append_faq_block( $content ) { if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() ) { return $content; } $faq = $this->get_faq( get_the_ID() ); if ( empty( $faq ) ) { return $content; } $html = "\n" . '
'; $html .= '

' . esc_html__( 'Frequently asked questions', 'admetric' ) . '

'; foreach ( $faq as $item ) { $html .= '

' . esc_html( $item['question'] ) . '

'; $html .= '

' . esc_html( $item['answer'] ) . '

'; } $html .= '
'; return $content . $html; } /** * Read and validate FAQ meta. * * @param int $post_id Post ID. * @return array */ private function get_faq( $post_id ) { $faq = get_post_meta( (int) $post_id, AdMetric_Publisher::META_FAQ, true ); if ( ! is_array( $faq ) ) { return array(); } $clean = array(); foreach ( $faq as $item ) { if ( is_array( $item ) && ! empty( $item['question'] ) && ! empty( $item['answer'] ) ) { $clean[] = array( 'question' => (string) $item['question'], 'answer' => (string) $item['answer'], ); } } return $clean; } }