Php 为FAQ页面动态设置架构数据

Php 为FAQ页面动态设置架构数据,php,json,wordpress,schema,Php,Json,Wordpress,Schema,我试图使用每个问题和答案的类名动态设置FAQ页面上所有问题和答案的模式数据。我不能使用脚本,因为它被Yoast的SEO覆盖 到目前为止,我已经创建了下面的代码,但是它没有将问题和答案引入到Google的结构化数据工具中,并且我无法循环每个问题和答案 HTML 您可以在特定页面上禁用Yoast,然后可以将自己的模式排队 从Yoast SEO 14.0开始,我们已经改变了您与 Yoast SEO的输出。但是,在某些情况下,您可能希望禁用 来自Yoast SEO的特定帖子的输出 更多信息@您可以在

我试图使用每个问题和答案的类名动态设置FAQ页面上所有问题和答案的模式数据。我不能使用脚本,因为它被Yoast的SEO覆盖

到目前为止,我已经创建了下面的代码,但是它没有将问题和答案引入到Google的结构化数据工具中,并且我无法循环每个问题和答案

HTML
您可以在特定页面上禁用Yoast,然后可以将自己的模式排队

从Yoast SEO 14.0开始,我们已经改变了您与 Yoast SEO的输出。但是,在某些情况下,您可能希望禁用 来自Yoast SEO的特定帖子的输出



更多信息@

您可以在特定页面上禁用Yoast,然后您可以将自己的模式排队

从Yoast SEO 14.0开始,我们已经改变了您与 Yoast SEO的输出。但是,在某些情况下,您可能希望禁用 来自Yoast SEO的特定帖子的输出



更多信息@

谢谢,我来试一试。谢谢,我来试一试。
<div class="x-acc-item">
          <span class="question">Can I order for pick up or delivery? 
          </span>

    <div class="answer">
      <p>Yes, you can!</p>
    </div>
</div>

<div class="x-acc-item">
      <span class="question">Do you have a rewards program?</span>

    <div class="answer">
      <p>We sure do!</p>
    </div>
</div>
add_filter( 'wpseo_schema_webpage', 'change_faq_schema' );


function change_faq_schema( $data ) {
    if ( ! is_page( 'frequently-asked-questions' ) ) {
        return $data;
    }

    $schema = array(
    'mainEntity' => array()
    );

    $resultquestion = array();
    $resultanswer = array();
    $question = "question";
    $answer = "answer";
    $domdocument = new DOMDocument();
    $domdocument->loadHTML($doc);
    $a = new DOMXPath($domdocument);
    $spans = $a->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $question ')]");
    $spans2 = $a->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $answer ')]");


    for ($i = $spans->length - 1; $i > -1; $i--) {
        $resultquestion[] = $spans->item($i)->firstChild->nodeValue;
    }

    for ($i = $spans2->length - 1; $i > -1; $i--) {
        $resultanswer[] = $spans2->item($i)->firstChild->nodeValue;
    }


    $data['mainEntity'] = array(
                    '@type' => "Question",
                    'name'   => $resultquestion,
                    "acceptedAnswer" => array(
                                            "@type" => "Answer",
                                            "text" => $resultanswer
                                            )
                );

    return $data;
}
<?php
add_action( 'template_redirect', 'remove_wpseo' );
/**
 * Removes output from Yoast SEO on the frontend for a specific post, page or custom post type.
 */
function remove_wpseo() {
    if ( is_single ( 1 ) ) {
        $front_end = YoastSEO()->classes->get( Yoast\WP\SEO\Integrations\Front_End_Integration::class );

        remove_action( 'wpseo_head', [ $front_end, 'present_head' ], -9999 );
    }
}; ?>