【发布时间】:2019-01-12 23:45:33
【问题描述】:
我有一堆 JSON-LD 文件,我想注入到各个页面的头部,其中一些是有条件的。我正在使用 WordPress,他们的建议是使用像 wp_enqueue_script 这样的函数。困难在于我还没有找到使用wp_enqueue_script 函数编辑<type> 属性的方法。
我可以像这样用 PHP 笨拙地做到这一点(也许有更好的 PHP 函数?):
// header.php
<head>
...all the regular stuff..
..near the bottom..
<?php
if ( is_front_page() ) {
echo file_get_contents(get_template_directory() . '/assets/structured-data/local-business.js');
}
?>
</head>
我尝试注入的脚本格式如下:
// JSON-LD format
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "localBusiness",
"currenciesAccepted": ...
}
</script>
所以我只是想读取其中的文件。当我尝试包含多个脚本时,这会中断。
我想遵循 WP 的建议并使用像 wp_enqueue_script() 这样的方法,但到目前为止,我还没有找到这样做的方法。除非我可以将脚本标记为 JSON-LD 所需的 type="application/ld+json",否则整个函数将无法工作。
【问题讨论】:
-
我决定暂时将它们硬编码到 header.php 文件中。 new core WP function 也正在开发中,以更改脚本/样式标签生成上的
attr值,希望很快就会推出。
标签: javascript wordpress schema json-ld structured-data