Skip to content

Instantly share code, notes, and snippets.

@itchief
Created August 14, 2022 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save itchief/2bca1460dcc7aa2e11d9999009e7b6d2 to your computer and use it in GitHub Desktop.
Save itchief/2bca1460dcc7aa2e11d9999009e7b6d2 to your computer and use it in GitHub Desktop.
JSON-LD breadcrumb for MODX
<?php
$siteId = $modx->getOption('site_start');
$siteUrl = $modx->getOption('site_url');
// добавляем текущую страницу
$crumbs[] = ['name' => $modx->resource->pagetitle, 'item' => $modx->makeUrl($modx->resource->id, '', '', 'full')];
// получаем родительский ресурс
$parent = $modx->getObject('modResource', $modx->resource->parent);
// пока родительский ресурс не равен null выполняем...
while ($parent != null) {
$item = $parent->id == $siteId ? $siteUrl : $modx->makeUrl($parent->id, '', '', 'full');
$crumbs[] = ['name' => $parent->menutitle, 'item' => $item];
if ($parent->id == $siteId) {
break;
}
$parent = $modx->getObject('modResource', $parent->parent);
if ($parent == null) {
$parent = $modx->getObject('modResource', $siteId);
}
}
$bredcrumbs = [];
$index = 1;
for ($i = count($crumbs) - 1; $i >= 0; $i--) {
$bredcrumbs[] = [
'@type' => 'ListItem',
'position' => $index++,
'name' => $crumbs[$i]['name'],
'item' => $crumbs[$i]['item']
];
}
$output = json_encode([[
'@context' => 'https://schema.org',
'@type' => 'BreadcrumbList',
'itemListElement' => $bredcrumbs
]], JSON_UNESCAPED_UNICODE);
return '<script type="application/ld+json">' . $output . '</script>';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment