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!
- By using a for loop statement
- By using an each statement
- By using an array ending with .get()
- By using AJAX requests
and then use the join(',', var) to assemble your JSON strings with commas. If you are not sure how to do it, you can always create a new service request in the Commercial Support section of the forum to get this done as a custom job.
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
Hi devken, found a solution?devken wrote: ↑Wed Aug 29, 2018 9:27 pmHello,
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!
The easiest way is to add all elements to array instead of directly displaying them and then use implode() function.
Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com
Then don't program anything.
Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com
The solution has already been provided when those moments happens: viewtopic.php?f=190&t=206686#p733126 .
Dedication and passion goes to those who are able to push and merge a project.
Regards,
Straightlight
Programmer / Opencart Tester
Or a sixth: hire by yourself a developer ...
Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.
https://www.opencart.com/index.php?rout ... n_id=22943
The stuctured data is very tricky and Google changes the requriments 5-6 times per year.
Suppliers Module - XML, CSV, XLS Product Feed Import and Update
Rich Snippets | Facebook Open Graph Meta Tags | WebP Images
Is this extension work with 1.5.6.4?agatha65 wrote: ↑Sat Feb 01, 2020 4:55 am@nobreak, what about my extension?
https://www.opencart.com/index.php?rout ... n_id=22943
The stuctured data is very tricky and Google changes the requriments 5-6 times per year.
- Count elements in breadcrumbs
- substract 1 ( for starts at 0, not 1 )
- echo all but last with trailing comma
- echo last without ,
Code: Select all
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "CollectionPage","breadcrumb":
{"@type": "BreadcrumbList","ItemListElement":
[
<?php $crumblies = (count($breadcrumbs)-1); ?>
<?php for ($crumbly = 0; $crumbly < $crumblies; $crumbly++) { ?>
{"@type":"ListItem","position":<?php echo $crumbly; ?>,"item":
{"@id":"<?php echo $breadcrumbs[$crumbly]['href']; ?>","name":"<?php echo $breadcrumbs[$crumbly]['text']; ?>"}},
<?php } ?>
{"@type":"ListItem","position":<?php echo $crumblies; ?>,"item":
{"@id":"<?php echo $breadcrumbs[$crumblies]['href']; ?>","name":"<?php echo $breadcrumbs[$crumblies]['text']; ?>"}}
]
}}
</script>
Because - why?!nobreak wrote: ↑Sat Feb 01, 2020 8:38 amIs this extension work with 1.5.6.4?agatha65 wrote: ↑Sat Feb 01, 2020 4:55 am@nobreak, what about my extension?
https://www.opencart.com/index.php?rout ... n_id=22943
The stuctured data is very tricky and Google changes the requriments 5-6 times per year.
This here is a thread about OpenCart 2.x .. NOT 1.5.x
Full Stack Web Developer :: Dedicated OpenCart Development & Support DACH Region
Contact for Custom Work / Fast Support.
Users browsing this forum: No registered users and 201 guests