【发布时间】:2019-12-30 00:33:15
【问题描述】:
我正在为 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;
)
}
但是这根本不起作用。如果有人能指出在这种情况下工作的嵌套实体的正确方向,我将不胜感激。
【问题讨论】:
-
你有没有想过这个问题?出于完全相同的原因,我目前正在尝试做同样的事情。
-
@TraceDeCoy 不幸的是,这个项目被搁置了一段时间,所以我从来没有深入研究它。然而,在随后的项目中,我采用了不同的方法并将脚本设置为页面本身的一部分,而不是尝试通过函数文件来完成。这个页面有不同的内容,因此他的架构是不同的,但我会有一个小提琴,看看是否可以在这里应用。
-
@TraceDeCoy 忽略我上面所说的。我正在考虑一个完全不同的项目:)。自从我这样做以来已经很久了,我完全忘记了。我确实解决了它,并在下面发布了我的解决方案。
-
太棒了,感谢下方分享。
标签: php wordpress schema advanced-custom-fields json-ld