You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
Fakel-Gym/navigation-menu.php

180 lines
7.7 KiB

<?php
/**
* Навигационное меню с сео
* По умолчанию названию меню Шапка сайта
*/
$menu = wp_get_nav_menu_object('Шапка сайта');
if ($menu) {
$menu_items = wp_get_nav_menu_items($menu->term_id);
$current_lang = function_exists('pll_current_language') ? pll_current_language() : null;
if ($menu_items) {
// Чистим заголовок от (переводов)
$clean_title = function($title) {
return trim(preg_replace('/\s*\([^)]*\)/', '', $title));
};
$current_region = 'RU';
// Навигационная схема для Яндекса
$navigation_schema = array(
"@context" => "https://schema.org",
"@type" => "SiteNavigationElement",
"name" => "Главная навигация сайта",
"description" => "Основное меню навигации по сайту",
"inLanguage" => $current_lang ?: 'ru',
"areaServed" => $current_region,
"url" => array()
);
// Хлебные крошки для Яндекса
$breadcrumb_schema = array(
"@context" => "https://schema.org",
"@type" => "BreadcrumbList",
"itemListElement" => array()
);
?>
<nav class="main-navigation primary-navigation h-full"
role="navigation"
aria-label="Основное меню сайта"
itemscope
itemtype="https://schema.org/SiteNavigationElement"
data-region="<?php echo esc_attr($current_region); ?>"
data-lang="<?php echo esc_attr($current_lang ?: 'ru'); ?>">
<p class="screen-reader-text">Главное меню</p>
<ul class="flex items-center h-full gap-[30px] justify-between nav-menu main-menu" role="menubar" aria-label="Главная навигация">
<?php
$position = 1;
foreach ($menu_items as $index => $item) {
$url = $item->url;
$title = $clean_title($item->title);
// Polylang
if ($current_lang && $item->object == 'page' && function_exists('pll_get_post')) {
$translated_page_id = pll_get_post($item->object_id, $current_lang);
if ($translated_page_id) {
$url = get_permalink($translated_page_id);
$translated_title = get_the_title($translated_page_id);
if ($translated_title) {
$title = $clean_title($translated_title);
}
}
}
// Дата изменения для Яндекса
$last_modified = '';
if ($item->object == 'page') {
$last_modified = get_the_modified_time('c', $item->object_id);
}
// Активный пункт меню
$is_current = false;
$current_classes = '';
$aria_current = '';
if (is_page()) {
global $post;
if ($item->object == 'page') {
if (($current_lang && function_exists('pll_get_post') && pll_get_post($item->object_id, $current_lang) == $post->ID) ||
$item->object_id == $post->ID) {
$is_current = true;
$current_classes = ' current-menu-item active current_page_item';
$aria_current = ' aria-current="page"';
}
}
}
// CSS классы
$css_classes = array(
'menu-item',
'menu-item-' . $item->ID,
'menu-item-type-' . $item->type,
'menu-item-object-' . $item->object,
'menu-order-' . $item->menu_order
);
if ($is_current) {
$css_classes[] = 'current-menu-item';
$css_classes[] = 'current_page_item';
$css_classes[] = 'active';
}
if (!empty($item->classes)) {
$css_classes = array_merge($css_classes, array_filter($item->classes));
}
// Схема для Яндекса
$page_data = array(
"@type" => "WebPage",
"name" => $title,
"url" => $url,
"position" => $position
);
if ($last_modified) {
$page_data['dateModified'] = $last_modified;
}
$navigation_schema['url'][] = $page_data;
$breadcrumb_schema['itemListElement'][] = array(
"@type" => "ListItem",
"position" => $position,
"name" => $title,
"item" => $url
);
?>
<li class="hover:translate-y-[-1px] transition h-full <?php echo esc_attr(implode(' ', $css_classes)); ?>" role="none">
<a href="<?php echo esc_url($url); ?>"
class="menu-link !no-underline h-full flex items-center"
role="menuitem"
itemprop="url"
tabindex="0"<?php echo $aria_current; ?>
title="<?php echo esc_attr('Перейти на страницу: ' . $title); ?>"
data-menu-item="<?php echo esc_attr($title); ?>"
data-menu-position="<?php echo $position; ?>"
onclick="if(typeof yaCounter !== 'undefined') { yaCounter.reachGoal('menu_click', {item: '<?php echo esc_js($title); ?>'}); }"
<?php if ($item->target): ?>target="<?php echo esc_attr($item->target); ?>"<?php endif; ?>
<?php if ($item->attr_title): ?>title="<?php echo esc_attr($item->attr_title); ?>"<?php endif; ?>>
<span itemprop="name"><?php echo esc_html($title); ?></span>
</a>
</li>
<?php
// Сепаратор
if ($index < count($menu_items) - 1): ?>
<li class="menu-separator" role="none" aria-hidden="true">
<span class="separator"><svg width="7" height="15" viewBox="0 0 7 15" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M0.574219 14.4345L5.55022 0.5625H6.92622L1.93422 14.4345H0.574219Z" fill="#E0E0E0"/>
</svg>
</span>
</li>
<?php endif;
$position++;
} ?>
</ul>
</nav>
<?php
// Cео
?>
<script type="application/ld+json">
<?php echo json_encode($navigation_schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>
</script>
<script type="application/ld+json">
<?php echo json_encode($breadcrumb_schema, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); ?>
</script>
<?php
}
}
?>