Meta Tags Plugin

Thanks for the love guys!

@jimbobrjames you can use schema available at schema.org, just pass a multidimensional array to json-ld and the plugin will take care of the rest, like so:

c::set('meta-tags.default', function ($page, $site) {
    $pages = $site->pages()->visible();
    $breadcrumb = [];

    foreach ($pages as $p) {
        $breadcrumb[] = [
            '@type'    => 'ListItem',
            'position' => $p->num(),
            'item'     => [
                '@id'  => $p->url(),
                'name' => $p->title()->value(),
            ],
        ];
    }

    return [
        'title'   => $page->title(),
        'meta'    => [/* ... */],
        'link'    => [/* ... */],
        'og'      => [/* ... */],
        'twitter' => [/* ... */],
        'json-ld' => [
            'BreadcrumbList' => [
                'itemListElement' => $breadcrumb
            ]
        ]
    ];
});

The output when you call $page->metaTags() will be:

<script type="application/ld+json">
{
    "@context": "http://schema.org",
    "@type": "BreadcrumbList",
    "itemListElement": [
        {
            "@type": "ListItem",
            "position": "1",
            "item": {
                "@id": "https://example.com",
                "name": "Homepage"
            }
        },
        {
            "@type": "ListItem",
            "position": "2",
            "item": {
                "@id": "https://tbm.test/about",
                "name": "About Us"
            }
        }
    ]
}
</script>

The first level @context and @type can be omitted and the plugin will include them for you, but you have to include the @type property for nested items.

1 Like