我知道在默认情况下,WordLift会在页面加载AJAX请求后异步发布JSON-LD,然后将JSON-LD注入页面头部。
然而,我更喜欢由WordPress同步加载JSON-LD并避免AJAX调用,我该怎么做呢?
发布于 2018-12-22 03:21:37
为了将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 );https://stackoverflow.com/questions/53889789
复制相似问题