Hello,
I am creating schema.org JSON+LD markup for my opencart. I’m using the repeater for breadcrumbs and it’s working wonderfully, however to get valid markup I need to remove the comma from the last repeater/breadcrumb item. Here’s my code:
<script type=“application/ld+json”>
{
“@context”: “http://schema.org”,
“@type”: “CollectionPage”,“breadcrumb”:
{"@type": “BreadcrumbList”,“ItemListElement”:
[
<?php $crumbly = 0; ?>
<?php foreach ($breadcrumbs as $breadcrumb):
$crumbly++;
?>
{"@type":“ListItem”,“position”:<?php echo $crumbly; ?>,“item”:
{"@id":"<?php echo $breadcrumb[‘href’]; ?>",“name”:"<?php echo $breadcrumb[‘text’]; ?>"}},
<?php endforeach ?>
]
}}
</script>
…
The result if there were just two breadcrumb items (minus the comma on the last item) should be:
<script type=“application/ld+json”>
{"@context":“http://schema.org","@type”:“BreadcrumbList”,“itemListElement”:
[{"@type":“ListItem”,“position”:1,“item”:
{"@id":“https://example.com/",“name”:"Home”}}, // <-- Comma required because there’s another list item to follow this one.
{"@type":“ListItem”,“position”:2,“item”:
{"@id":“https://example.com/MyCategory",“name”:"MyCategory”}} // <-- No comma required as this is the last list item. However, as per my code, it will show a comma here.
]}
</script>
Please advise how I can modify my script. Thanks in advance!