# Schema Markup (JSON-LD) breadcrumbs

**URL:** <https://forum.getkirby.com/t/schema-markup-json-ld-breadcrumbs/10553>\
**Category:** Questions\
**Tags:** v2\
**Created:** [June 2, 2018, 8:46pm UTC](https://forum.getkirby.com/t/schema-markup-json-ld-breadcrumbs/10553 "2018-06-02T20:46:14Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [June 2, 2018, 8:46pm UTC](https://forum.getkirby.com/t/schema-markup-json-ld-breadcrumbs/10553/1 "2018-06-02T20:46:14Z")

</div>

Hey,

Recently I started with Kirby and I really like it!  
I am a starting developer and therefore I have to learn a lot. I have already searched the forum if I could find an answer for my problem, but I had no success.

Maybe you can help me:

I want to make my breadcrumbs as a JSON-LD markup. I have already the follow code:

```
<script type="application/ld+json">
{
  "@context": "http://schema.org/", 
  "@type": "BreadcrumbList", 
  "itemListElement": [
        <?php foreach($site->breadcrumb() as $crumb): ?>
    { 
      "@type": "ListItem", 
      "position": "1", 
      "item": { 
        "@id": "<?= $crumb->url() ?>", 
        "name": "<?= $crumb->title() ?>" 
      } 
    },

        <?php endforeach ?>

  ]
}
</script>

```

It works. But I want to count the positions as well. Do you have an idea how to do this?

At last I need to seperate the items with a comma (except the last one). How can I do this?

Thanks!  
Porto

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [June 2, 2018, 9:31pm UTC](https://forum.getkirby.com/t/schema-markup-json-ld-breadcrumbs/10553/2 "2018-06-02T21:31:20Z")

</div>

Welcome 🙂

I did it like this… you have to add 1 because the index will start at 0 because thats where PHP starts counting from, but the schema position needs to start at 1. You can use `last()` to handle the no comma on the last entry.

```auto
<script type="application/ld+json">
{
 "@context": "http://schema.org",
 "@type": "BreadcrumbList",
 "itemListElement":
 [
   <?php
   foreach ($site->breadcrumb() as $crumb):
   $index = $site->breadcrumb()->indexOf($crumb);
   $depth = $index + 1;
   ?>
  {
   "@type": "ListItem",
   "position": <?= $depth ?>,
   "item":
   {
    "name": "<?= $crumb->title() ?>",
    "@id": "<?= $crumb->url() ?>"
    }
    <?php if($crumb->is($site->breadcrumb()->last())): ?>}<?php else: ?>},<?php endif ?>
  <?php endforeach ?>
 ]
}
</script>

```

---

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [June 2, 2018, 9:46pm UTC](https://forum.getkirby.com/t/schema-markup-json-ld-breadcrumbs/10553/3 "2018-06-02T21:46:01Z")

</div>

Wow, that’s fast!  
You helped me a lot 😀

After endless puzzling, sometimes you need some help to find the solution.

Thank you very much!!!

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [June 2, 2018, 10:02pm UTC](https://forum.getkirby.com/t/schema-markup-json-ld-breadcrumbs/10553/4 "2018-06-02T22:02:51Z")

</div>

No worries. I cleaned it up a little… its a little more readable and less repeaty…

```auto
<script type="application/ld+json">
{
 "@context": "http://schema.org",
 "@type": "BreadcrumbList",
 "itemListElement":
 [
   <?php
   $crumbs = $site->breadcrumb();
   foreach ($crumbs as $crumb):
     $index = $crumbs->indexOf($crumb);
     $depth = $index + 1;
     $last = $crumb->is($crumbs->last()) ? '}' : '},';
   ?>
  {
   "@type": "ListItem",
   "position": <?= $depth ?>,
   "item":
   {
    "name": "<?= $crumb->title() ?>",
    "@id": "<?= $crumb->url() ?>"
    }
    <?= $last ?>
  <?php endforeach ?>
 ]
}
</script>

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [June 3, 2018, 8:10am UTC](https://forum.getkirby.com/t/schema-markup-json-ld-breadcrumbs/10553/5 "2018-06-03T08:10:07Z")

</div>

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

```auto
<?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:

> **[Torann/json-ld](https://github.com/Torann/json-ld)**
>
> json-ld - Extremely simple JSON-LD generator.

---

<div class="post-metadata">

**Author:** ![portobien](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/portobien/32/7162_2.png) [@portobien](https://forum.getkirby.com/u/portobien)\
**Post date:** [June 3, 2018, 1:08pm UTC](https://forum.getkirby.com/t/schema-markup-json-ld-breadcrumbs/10553/6 "2018-06-03T13:08:24Z")

</div>

Thank you all for your help! It was very helpful 🙂

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [September 1, 2019, 11:43am UTC](https://forum.getkirby.com/t/schema-markup-json-ld-breadcrumbs/10553/7 "2019-09-01T11:43:22Z")

</div>

Sorry to dig up this old thread, but i’ve just used the example from @texnixe (which was an improved version of mine, so kinda my fault) and realised a problem with the logic. The Position key denotes the position in the site, which is not necessarily the position in the index. For some of my pages i’m getting a position of 7, when actually it should be 4. Heres an example from google…

```auto
{
  "@context": "https://schema.org",
  "@type": "BreadcrumbList",
  "itemListElement": [{
    "@type": "ListItem",
    "position": 1,
    "name": "Books",
    "item": "https://example.com/books"
  },{
    "@type": "ListItem",
    "position": 2,
    "name": "Authors",
    "item": "https://example.com/books/authors"
  },{
    "@type": "ListItem",
    "position": 3,
    "name": "Ann Leckie",
    "item": "https://example.com/books/authors/annleckie"
  },{
    "@type": "ListItem",
    "position": 4,
    "name": "Ancillary Justice",
    "item": "https://example.com/books/authors/ancillaryjustice"
  }]
}

```

How can i get the pages depth in the site?

---

<div class="post-metadata">

**Author:** ![anon77445132](https://avatars.discourse-cdn.com/v4/letter/a/4da419/32.png) [@anon77445132](https://forum.getkirby.com/u/anon77445132)\
**Post date:** [September 1, 2019, 11:53am UTC](https://forum.getkirby.com/t/schema-markup-json-ld-breadcrumbs/10553/8 "2019-09-01T11:53:39Z")

</div>

Some time ago I wrote to this question at [https://forum.getkirby.com/t/meta-tags-plugin/6802/10](https://forum.getkirby.com/t/meta-tags-plugin/6802/10)

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [September 1, 2019, 11:53am UTC](https://forum.getkirby.com/t/schema-markup-json-ld-breadcrumbs/10553/9 "2019-09-01T11:53:48Z")

</div>

Answered my own question…

```auto
<?php

$itemListElement = [];

foreach($site->breadcrumb() as $crumb) {

$index = $site->breadcrumb()->indexOf($crumb);
$depth = $index + 1;

  $itemListElement[] = [
    '@type' => 'listItem',
    'position' => $depth,
    'item' => [
      'name' => $crumb->title()->value(),
      '@id' => $crumb->url()
    ]
  ];
}

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

?>
<script type="application/ld+json">
<?= json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); ?>
</script>

```

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:23pm UTC](https://forum.getkirby.com/t/schema-markup-json-ld-breadcrumbs/10553/10 "2025-02-06T17:23:08Z")

</div>


