首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为FAQ页动态设置架构数据

为FAQ页动态设置架构数据
EN

Stack Overflow用户
提问于 2020-12-30 02:26:25
回答 1查看 141关注 0票数 1

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

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

HTML

代码语言:javascript
复制
<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>

PHP

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2020-12-30 02:41:46

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

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

代码语言:javascript
复制
<?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 );
    }
}; ?>

更多信息@ https://developer.yoast.com/customization/yoast-seo/disabling-yoast-seo/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65496929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档