首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在页眉中发布WordLift的JSON-LD

如何在页眉中发布WordLift的JSON-LD
EN

Stack Overflow用户
提问于 2018-12-22 03:21:37
回答 1查看 104关注 0票数 0

我知道在默认情况下,WordLift会在页面加载AJAX请求后异步发布JSON-LD,然后将JSON-LD注入页面头部。

然而,我更喜欢由WordPress同步加载JSON-LD并避免AJAX调用,我该怎么做呢?

EN

回答 1

Stack Overflow用户

发布于 2018-12-22 03:21:37

为了将WordLift的JSON-LD转换为异步,您可以在主题的functions.php文件中添加以下内容:

代码语言:javascript
运行
复制
// Disable the asynchronous JSON-LD.
add_filter( 'wl_jsonld_enabled', function () {
    return false;
} , 10, 0 );

// Hook to the `wp_head` to output the JSON-LD.
add_action( 'wp_head', function () {
    // Check that the Wordlift_Jsonld_Service exists.
    if ( ! class_exists( 'Wordlift_Jsonld_Service' ) ) {
        echo '<!-- WordLift JSON-LD service not found. -->';

        return;
    }

    // Determine whether this is the home page or whether we're displaying a single post.
    $is_homepage = is_home() || is_front_page();
    $post_id     = is_singular() ? get_the_ID() : null;

    // Get the JSON-LD.
    $jsonld = json_encode( Wordlift_Jsonld_Service::get_instance()
        ->get_jsonld( $is_homepage, $post_id ) );
    // Finally print the JSON-LD out.
    ?>
    <script type="application/ld+json"><?php echo $jsonld; ?></script>
    <?php
}, 10, 0 );
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53889789

复制
相关文章

相似问题

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