【发布时间】:2018-12-21 19:21:36
【问题描述】:
我知道默认情况下,WordLift 在页面加载后使用 AJAX 请求异步发布 JSON-LD,然后将 JSON-LD 注入页面头部。
但是我更喜欢让 WordPress 同步加载 JSON-LD 并避免 AJAX 调用,我该怎么做?
【问题讨论】:
标签: wordpress json-ld wordlift
我知道默认情况下,WordLift 在页面加载后使用 AJAX 请求异步发布 JSON-LD,然后将 JSON-LD 注入页面头部。
但是我更喜欢让 WordPress 同步加载 JSON-LD 并避免 AJAX 调用,我该怎么做?
【问题讨论】:
标签: wordpress json-ld wordlift
为了将 WordLift 的 JSON-LD 转为异步,可以在主题的 functions.php 文件中添加以下内容:
// 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 );
【讨论】: