如何在 WordPress 中添加嵌套的 JSON-LD Schema 实体
How do I add nested JSON-LD Schema entities in WordPress
我没有使用插件,而是在为 WordPress 网站的主页创建自己的架构标记,该网站使用高级自定义字段 (ACF) 来处理与此挑战相关的一些内容.我的目标是让我对输出的内容进行更精细的控制,并作为一个小小的个人挑战:)
到目前为止,我已经成功创建了一个基本模式,但我在需要为服务列表创建嵌套实体的地方卡住了。
目前我的 functions.php 文件中有这个:
function schema() {
$schema = array(
'@context' => "http://schema.org",
'@type' => "ProfessionalService",
'name' => get_bloginfo('name'),
'url' => get_home_url(),
// ... and so on
);
if ( have_rows('services_list') ) {
$schema['itemListElement'] = array();
while (have_rows('services_list')) : the_row();
$services = array(
'@type' => 'Offer',
'itemOffered' => array (
'@type' => 'Service',
'name' => get_sub_field('service')
)
);
array_push($schema['itemListElement'], $services);
endwhile;
}
echo '<script type="application/ld+json">' . json_encode($schema) . '</script>';
}
add_action('wp_head', 'schema');
结果:
{
"@context":"http:\/\/schema.org",
"@type":"ProfessionalService",
"name":"Name of Company",
"url":"http:\/\/www.whatever.com",
and so on...
"itemListElement": [
{
"@type":"Offer",
"itemOffered": {
"@type":"Service",
"name":"Service One"
}
},
{
"@type":"Offer",
"itemOffered": {
"@type":"Service",
"name":"Service Two"
}
},
... more services
]
}
生成的标记很棒,但我需要嵌套 itemListElement 以便它输出如下:
"hasOfferCatalog": {
"@type": "OfferCatalog",
"name": "Some Services",
"itemListElement": [
{
"@type":"Offer",
"itemOffered": {
"@type":"Service",
"name":"Service One"
}
},
{
"@type":"Offer",
"itemOffered": {
"@type":"Service",
"name":"Service Two"
}
},
... more services
我一辈子都弄不明白这是怎么做到的。我目前最大的努力是像这样添加它:
if ( have_rows('services_list') ) {
'hasOfferCatalog' => array(
'@type' => 'OfferCatalog',
'name' => 'Tree Surgery'
$schema['itemListElement'] = array();
while (have_rows('services_list')) : the_row();
$services = array(
'@type' => 'Offer',
'itemOffered' => array (
'@type' => 'Service',
'name' => get_sub_field('service')
)
);
array_push($schema['itemListElement'], $services);
endwhile;
)
}
但是这根本不起作用。如果有人能为我指出在这种情况下工作的嵌套实体的正确方向,我将不胜感激。
我最终设法解决了我的问题。当时我一直没有时间发布这个,但由于有人对此感兴趣,所以我就这样做了。
我的 'best effort' 差不多了,但我需要做一些小的调整。遗憾的是,我前段时间解决了这个问题,但我忘记了我用来纠正问题的资源,但我希望这可能对其他人有所帮助。
if ( have_rows('services_list') ) {
$schema['hasOfferCatalog'] = array();
$catalog = array(
'@type' => 'OfferCatalog',
'name' => 'Tree Surgery'
);
if ( have_rows('services_list') ) {
$catalog['itemListElement'] = array();
while (have_rows('services_list')) : the_row();
$services = array(
'@type' => 'Offer',
'itemOffered' => array (
'@type' => 'Service',
'name' => get_sub_field('service')
)
);
array_push($catalog['itemListElement'], $services);
endwhile;
array_push($schema['hasOfferCatalog'], $catalog);
}
}
Fr 一些上下文我把这些都放在我的 functions.php 文件中,并且像这样放在一起:
function schema() {
$schema = array(
'@context' => "http://schema.org",
'@type' => "ProfessionalService",
'name' => get_bloginfo('name'),
'url' => get_home_url(),
'telephone' => '+00 0000 00000',
'address' => array(
'@type' => 'PostalAddress',
'streetAddress' => 'XXXXX',
'postalCode' => 'XXX XXX',
'addressLocality' => 'XXXXXX',
'addressRegion' => 'XXXXXXX',
'addressCountry' => 'XXXXXXXXXXXX'
),
'logo' => get_stylesheet_directory_uri() . '/path/to/your/image.svg',
'image' => get_stylesheet_directory_uri() . '/path/to/your/image.svg'
);
if ( have_rows('services_list') ) {
$schema['hasOfferCatalog'] = array();
$catalog = array(
'@type' => 'OfferCatalog',
'name' => 'Tree Surgery'
);
if ( have_rows('services_list') ) {
$catalog['itemListElement'] = array();
while (have_rows('services_list')) : the_row();
$services = array(
'@type' => 'Offer',
'itemOffered' => array (
'@type' => 'Service',
'name' => get_sub_field('service')
)
);
array_push($catalog['itemListElement'], $services);
endwhile;
array_push($schema['hasOfferCatalog'], $catalog);
}
}
echo '<script type="application/ld+json">' . json_encode($schema) . '</script>';
}
add_action('wp_head', 'schema');
它似乎能胜任。
我没有使用插件,而是在为 WordPress 网站的主页创建自己的架构标记,该网站使用高级自定义字段 (ACF) 来处理与此挑战相关的一些内容.我的目标是让我对输出的内容进行更精细的控制,并作为一个小小的个人挑战:)
到目前为止,我已经成功创建了一个基本模式,但我在需要为服务列表创建嵌套实体的地方卡住了。
目前我的 functions.php 文件中有这个:
function schema() {
$schema = array(
'@context' => "http://schema.org",
'@type' => "ProfessionalService",
'name' => get_bloginfo('name'),
'url' => get_home_url(),
// ... and so on
);
if ( have_rows('services_list') ) {
$schema['itemListElement'] = array();
while (have_rows('services_list')) : the_row();
$services = array(
'@type' => 'Offer',
'itemOffered' => array (
'@type' => 'Service',
'name' => get_sub_field('service')
)
);
array_push($schema['itemListElement'], $services);
endwhile;
}
echo '<script type="application/ld+json">' . json_encode($schema) . '</script>';
}
add_action('wp_head', 'schema');
结果:
{
"@context":"http:\/\/schema.org",
"@type":"ProfessionalService",
"name":"Name of Company",
"url":"http:\/\/www.whatever.com",
and so on...
"itemListElement": [
{
"@type":"Offer",
"itemOffered": {
"@type":"Service",
"name":"Service One"
}
},
{
"@type":"Offer",
"itemOffered": {
"@type":"Service",
"name":"Service Two"
}
},
... more services
]
}
生成的标记很棒,但我需要嵌套 itemListElement 以便它输出如下:
"hasOfferCatalog": {
"@type": "OfferCatalog",
"name": "Some Services",
"itemListElement": [
{
"@type":"Offer",
"itemOffered": {
"@type":"Service",
"name":"Service One"
}
},
{
"@type":"Offer",
"itemOffered": {
"@type":"Service",
"name":"Service Two"
}
},
... more services
我一辈子都弄不明白这是怎么做到的。我目前最大的努力是像这样添加它:
if ( have_rows('services_list') ) {
'hasOfferCatalog' => array(
'@type' => 'OfferCatalog',
'name' => 'Tree Surgery'
$schema['itemListElement'] = array();
while (have_rows('services_list')) : the_row();
$services = array(
'@type' => 'Offer',
'itemOffered' => array (
'@type' => 'Service',
'name' => get_sub_field('service')
)
);
array_push($schema['itemListElement'], $services);
endwhile;
)
}
但是这根本不起作用。如果有人能为我指出在这种情况下工作的嵌套实体的正确方向,我将不胜感激。
我最终设法解决了我的问题。当时我一直没有时间发布这个,但由于有人对此感兴趣,所以我就这样做了。
我的 'best effort' 差不多了,但我需要做一些小的调整。遗憾的是,我前段时间解决了这个问题,但我忘记了我用来纠正问题的资源,但我希望这可能对其他人有所帮助。
if ( have_rows('services_list') ) {
$schema['hasOfferCatalog'] = array();
$catalog = array(
'@type' => 'OfferCatalog',
'name' => 'Tree Surgery'
);
if ( have_rows('services_list') ) {
$catalog['itemListElement'] = array();
while (have_rows('services_list')) : the_row();
$services = array(
'@type' => 'Offer',
'itemOffered' => array (
'@type' => 'Service',
'name' => get_sub_field('service')
)
);
array_push($catalog['itemListElement'], $services);
endwhile;
array_push($schema['hasOfferCatalog'], $catalog);
}
}
Fr 一些上下文我把这些都放在我的 functions.php 文件中,并且像这样放在一起:
function schema() {
$schema = array(
'@context' => "http://schema.org",
'@type' => "ProfessionalService",
'name' => get_bloginfo('name'),
'url' => get_home_url(),
'telephone' => '+00 0000 00000',
'address' => array(
'@type' => 'PostalAddress',
'streetAddress' => 'XXXXX',
'postalCode' => 'XXX XXX',
'addressLocality' => 'XXXXXX',
'addressRegion' => 'XXXXXXX',
'addressCountry' => 'XXXXXXXXXXXX'
),
'logo' => get_stylesheet_directory_uri() . '/path/to/your/image.svg',
'image' => get_stylesheet_directory_uri() . '/path/to/your/image.svg'
);
if ( have_rows('services_list') ) {
$schema['hasOfferCatalog'] = array();
$catalog = array(
'@type' => 'OfferCatalog',
'name' => 'Tree Surgery'
);
if ( have_rows('services_list') ) {
$catalog['itemListElement'] = array();
while (have_rows('services_list')) : the_row();
$services = array(
'@type' => 'Offer',
'itemOffered' => array (
'@type' => 'Service',
'name' => get_sub_field('service')
)
);
array_push($catalog['itemListElement'], $services);
endwhile;
array_push($schema['hasOfferCatalog'], $catalog);
}
}
echo '<script type="application/ld+json">' . json_encode($schema) . '</script>';
}
add_action('wp_head', 'schema');
它似乎能胜任。