如果您自己创建主题,则始终可以在主题本身中直接使用模式微数据标记。
正如我们在下面的示例中所看到的,向HTML添加额外属性可以使其符合模式。在本例中,我们使用itemscope
, itemtype
和itemprop
:
摘自http://schema.org/docs/gs.html#microdata_how
<div itemscope itemtype ="http://schema.org/Movie">
<h1 itemprop="name">Avatar</h1>
<div itemprop="director" itemscope itemtype="http://schema.org/Person">
Director: <span itemprop="name">James Cameron</span> (born <span itemprop="birthDate">August 16, 1954</span>)
</div>
<span itemprop="genre">Science fiction</span>
<a href="../movies/avatar-theatrical-trailer.html" itemprop="trailer">Trailer</a>
</div>
但是,如果要使用JSON-LD标记,那么可能需要考虑使用
PHP JSON-LD library 并通过将代码附加到
wp_head
功能:
从PHP-JSON-LD GitHub example:
<?php
// Describe your Thing to be marked up as an array
$doc = (object) array(
"http://schema.org/name" => "Manu Sporny",
"http://schema.org/url" => (object) array("@id" => "http://manu.sporny.org/"),
"http://schema.org/image" => (object) array("@id" => "http://manu.sporny.org/images/manu.png")
);
// Describe it\'s schema context as well
$context = (object)array(
"name" => "http://schema.org/name",
"homepage" => (object)array("@id" => "http://schema.org/url", "@type" => "@id"),
"image" => (object)array("@id" => "http://schema.org/image", "@type" => "@id")
);
function wp_json_ld($doc, $context) {
require_once(\'/path/to/library\');
// Pass it through this magical function
$compacted = jsonld_compact($doc, $context);
echo \'<script type="application/ld+json">\';
echo json_encode($compacted, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
echo \'</script>\';
}
/* Output:
<script type="application/ld+json">
{
"@context": {...},
"image": "http://manu.sporny.org/images/manu.png",
"homepage": "http://manu.sporny.org/",
"name": "Manu Sporny"
}
</script>
*/
// Add it to wp_head
add_action(\'wp_head\', \'wp_json_ld\');