我知道默认情况下,WordLift在页面加载后通过AJAX请求异步发布JSON-LD,然后将JSON-LD注入页面头部。
但是我更喜欢由WordPress同步加载JSON-LD并避免AJAX调用,我该怎么做?
答案 0 :(得分:0)
为了将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 );