mindkomm/theme-lib-structured-data

WordPress主题结构化数据辅助类

2.0.0 2019-01-23 09:49 UTC

This package is auto-updated.

Last update: 2024-09-12 05:00:21 UTC


README

WordPress主题结构化数据辅助类。

支持以下数据类型

安装

您可以通过Composer安装此包

composer require mindkomm/theme-lib-structured-data

使用方法

将生成的输出放入HTML的<head>部分。

add_action( 'wp_head', function() {
    // Local Business
    $local_business = new Theme\Structured_Data\Local_Business( $options );
    echo $local_business->generate_jsonld();

    // Website
    $website = new Theme\Structured_Data\Website();
    echo $website->generate_jsonld();

    // Logo
    $logo = new Theme\Structured_Data\Logo( $logo_url );
    echo $logo->generate_jsonld();

    // Social profiles
    $social_profiles = new Theme\Structured_Data\Social_Profile( $social_profiles );
    echo $social_profiles->generate_jsonld();
} );

网站

该类会检查是否可以在您的主题目录中找到search.php文件,并相应地包含一个潜在搜索操作属性。如果您想对此有更多的控制,可以向generate_jsonld()函数传递一个布尔值

// Use SearchAction, ignore check for search.php file.
echo $website->generate_jsonld( true );

本地商业

本地商业条目可以包含大量数据。以下类型的数据受到支持

  • 联系数据 – 地址、电话号码、电子邮件地址等。
  • 地理编码 – 商业在地图上所在位置的纬度和经度。
  • 营业时间 – 商业的营业时间。

您需要以数组的形式传递这些数据,在实例化类时。以下是您可以传递的所有选项概述

<?php

$options = [
    'name'                      => 'MIND Kommunikation GmbH',
    'street'                    => 'Wülflingerstrasse 36',
    'post_office_box_number'    => '',
    'zip'                       => '8400',
    'city'                      => 'Winterthur',
    'email'                     => 'hello@mind.ch',
    'phone'                     => '052 203 45 00',
    'country_code'              => 'CH',
    'logo'                      => 'https://www.mind.ch/our-logo.png',
    'social_profiles' => [
         'facebook'   => '',
         'twitter'    => 'https://twitter.com/mindkomm',
         'instagram'  => '',
         'youtube'    => '',
         'linkedin'   => '',
         'myspace'    => '',
         'pinterest'  => '',
         'soundcloud' => '',
         'tumblr'     => '',
     ],
    'location_map_url'          => 'https://www.google.ch/maps/place/MIND+Kommunikation+GmbH/@47.5054599,8.715229,17z/data=!3m1!4b1!4m5!3m4!1s0x479a999cb35b1801:0xefef1560f7750a4f!8m2!3d47.5054599!4d8.7174177',
    'geo'                       => [
        'address' => 'MIND Kommunikation GmbH, Wülflingerstrasse, Winterthur, Schweiz',
        'lat'     => '47.50545990000001',
        'lng'     => '8.717417699999942',
    ],
    'opening_hours'             => [
        [
            'weekday' => 'Monday',
            'opens'   => '08:00:00',
            'closes'  => '17:30:00',
        ],
        [
            'weekday' => 'Tuesday',
            'opens'   => '08:00:00',
            'closes'  => '17:30:00',
        ],
        [
            'weekday' => 'Wednesday',
            'opens'   => '08:00:00',
            'closes'  => '17:30:00',
        ],
        // …
    ],
    'opening_hours_closed'      => [
        [
            'date_from' => '2018-12-24',
            'date_to'   => '2019-01-02',
        ],
    ],
];

$local_business = new Theme\Structured_Data\Local_Business( $options );

echo $local_business->generate_jsonld();

徽标

根据徽标结构化数据指南添加徽标标记。

$logo = new Theme\Structured_Data\Logo( 'https://www.mind.ch/our-logo.png' );

echo $logo->generate_jsonld();

社交资料

根据社交资料结构化数据指南链接到社交媒体资料,如Facebook、Twitter、Instagram。

$social_profiles = new Theme\Structured_Data\Social_Profile( [
    'facebook'   => '',
    'twitter'    => 'https://twitter.com/mindkomm',
    'instagram'  => '',
    'youtube'    => '',
    'linkedin'   => '',
    'myspace'    => '',
    'pinterest'  => '',
    'soundcloud' => '',
    'tumblr'     => '',
] );

echo $social_profiles->generate_jsonld();

使用说明

  • 如何构建这些数据取决于您。您可以设置一个定制页面,或者可以使用ACF选项页面作为编辑这些值的界面。
  • 如果您没有为某项内容提供值,它将在输出中被忽略。
  • 始终使用结构化数据测试工具检查您的输出。

致谢

使用了Spatie的出色schema-org库。

支持

这是一个我们在MIND用于开发WordPress主题的库。您可以自由使用它,但我们目前不提供任何支持。