【问题标题】:How do I add nested JSON-LD Schema entities in WordPress如何在 WordPress 中添加嵌套的 JSON-LD Schema 实体
【发布时间】: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


【解决方案1】:

我最终设法解决了我的问题。当时我从来没有时间发布这个,但由于有点兴趣,这就是我所做的。

我的“最大努力”几乎完成了,但我需要做一些小的调整。遗憾的是,我前段时间解决了这个问题,我忘记了我用来纠正问题的资源,但我希望这可能对其他人有所帮助。

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);
    }
} 

从一点上下文来看,我把这些都放在了我的 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');

它似乎完成了这项工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-26
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-26
    • 2019-01-12
    • 1970-01-01
    相关资源
    最近更新 更多