我如何在WordPress中添加嵌套的JSON-LD Schema实体?

问题描述 投票:1回答:1

我没有使用插件,而是在为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');

Result:

{
    "@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;
    )
}

然而这根本行不通 如果有人能给我指出正确的方向,让我知道嵌套实体在这种情况下如何工作,我将非常感激。

php wordpress schema advanced-custom-fields json-ld
1个回答
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);
    }
} 

我把这些东西都放在了我的function.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');

看起来是这样的

© www.soinside.com 2019 - 2024. All rights reserved.