Schema Markup (JSON-LD) breadcrumbs

A probably better way of doing it, would be to create your complete array in PHP, then to json_encode().

<?php

$itemListElement = [];
foreach($site->children() as $page) {
  $itemListElement[] = [
    '@type' => 'listItem',
    'position' => $page->indexOf($pages) + 1,
    'item' => [
      'name' => $page->title()->value(),
      '@id' => $page->url()
    ]
  ];
}

$schema = [
  "@context" => "http://schema.org",
  "@type" => "BreadcrumbList",
  "itemListElement" => $itemListElement
];


?>
<script>
<?= json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT |  JSON_UNESCAPED_UNICODE); ?>
</script>

Cleaner, less error prone, no problem with the last comma, and you can keep your logic in the controller.

You might also consider the JSON generator:

3 Likes