Created
April 7, 2023 09:38
-
-
Save PechenkiUA/68b80e9796d44f555e5357f9714988a7 to your computer and use it in GitHub Desktop.
getJSONLD article
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// если на этой странице контент не является объектом "Article", то завершаем работу (на сайте статьи имеют шаблон с id = 2) | |
if ($modx->resource->get('template') !== 7) { | |
return; | |
} | |
// URL сайта | |
$siteUrl = $modx->getOption('site_url'); //$modx->getOption('http_host'); //$modx->getOption('site_url'); | |
// id страницы | |
$id = $modx->resource->get('id'); | |
// id главной страницы | |
$startId = $modx->getOption('site_start'); | |
// url страницы | |
// 1 способ | |
// $url = $modx->makeUrl($id, '', '', 'full'); | |
// 2 способ | |
if ($id == $startId) { | |
$url = $siteUrl; | |
} else { | |
$url = $siteUrl . $modx->resource->get('uri'); | |
} | |
// заголовок страницы | |
$title = mb_strimwidth($modx->resource->get('pagetitle'), 0, 110, '...'); | |
// описание страницы | |
$description = $modx->resource->get('description'); | |
// все изображения, расположенные в контенте страницы | |
$images = []; | |
preg_match_all('/<img\s+.*?src=[\"\']?([^\"\' >]*)[\"\']?[^>]*>/i', $modx->resource->get('content'), $matches, PREG_SET_ORDER); | |
foreach ($matches as $match) { | |
$image = $match[1]; | |
if (mb_strpos($image, '/') === 0) { | |
$image = $siteUrl . mb_substr($image, 1); | |
} elseif (mb_strpos($image, $siteUrl) !== 0) { | |
$image = $siteUrl . $image; | |
} | |
$images[] = $image; | |
} | |
// дата публикации статьи | |
$datePublished = date("c", strtotime($modx->resource->get('publishedon'))); | |
// дата последнего изменения статьи | |
if ($modx->resource->get('editedon')) { | |
$dateModified = date("c", strtotime($modx->resource->get('editedon'))); | |
} else { | |
$dateModified = $datePublished; | |
} | |
// автор статьи | |
$author = $modx->getObject('modUserProfile', $modx->resource->get('publishedby'))->get('fullname'); | |
// имя сайта | |
$siteName = $modx->getOption('site_name'); | |
// логотип сайта | |
$siteLogo = '/assets/images/logo.png'; | |
// хлебные крошки | |
$breadcrumbs = []; | |
$crumbs = []; | |
// добавлем текущую страницу | |
$crumbs[] = ['name' => $title, 'item' => $url]; | |
// получаем родительский ресурс | |
$parent = $modx->getObject('modResource', $modx->resource->get('parent')); | |
// пока родительский ресурс не равен null выполняем... | |
while ($parent != null) { | |
$item = $parent->get('id') == $startId ? $siteUrl : $siteUrl . $parent->get('uri'); | |
$crumbs[] = ['name' => $parent->get('menutitle'), 'item' => $item]; | |
if ($parent->get('id') == $startId) { | |
break; | |
} | |
$parent = $modx->getObject('modResource', $parent->get('parent')); | |
if ($parent == null) { | |
$parent = $modx->getObject('modResource', $startId); | |
} | |
} | |
$index = 1; | |
for ($i = count($crumbs) - 1; $i >= 0; $i--) { | |
$bredcrumbs[] = [ | |
'@type' => 'ListItem', | |
'position' => $index++, | |
'name' => $crumbs[$i]['name'], | |
'item' => $crumbs[$i]['item'] | |
]; | |
} | |
// создадим ассоциативный массив из данных | |
$data = [ | |
[ | |
'@context' => 'https://schema.org', | |
'@type' => 'Article', | |
'mainEntityOfPage' => [ | |
'@type' => 'WebPage', | |
'@id' => $url | |
], | |
'headline' => $title, | |
'image' => $images, | |
'datePublished' => $datePublished, | |
'dateModified' => $dateModified, | |
'author' => [ | |
'@type' => 'Person', | |
'name' => $author | |
], | |
'publisher' => [ | |
'@type' => 'Organization', | |
'name' => $siteName, | |
'logo' => [ | |
'@type' => 'ImageObject', | |
'url' => $siteLogo | |
] | |
], | |
'description' => $description | |
], [ | |
'@context' => 'https://schema.org', | |
'@type' => 'BreadcrumbList', | |
'itemListElement' => $bredcrumbs | |
] | |
]; | |
$json = json_encode($data, JSON_UNESCAPED_UNICODE); | |
echo sprintf('<script type="application/ld+json">%s</script>',$json); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment