'Вы находитесь здесь:',
'search_prefix' => 'Поиск:',
'not_found' => 'Страница не найдена',
'author_prefix' => 'Публикации автора',
'breadcrumb_nav' => 'Навигация по разделам'
);
$clean_title = function ($title) {
return trim(preg_replace('/\s*\([^)]*\)/', '', $title));
};
$separator_svg = '';
if (get_current_room() === 'gym') {
$home_icon_svg = '';
} else {
$home_icon_svg = '';
}
// Создаем элемент главной страницы отдельно
$home_breadcrumb = array(
"@type" => "ListItem",
"position" => 1,
"name" => $home_title,
"item" => $home_url
);
// Массив для остальных крошек (без главной)
$breadcrumbs = array();
$position = 2; // Начинаем с позиции 2, так как главная уже позиция 1
if (is_post_type_archive()) {
$post_type = get_queried_object();
$breadcrumbs[] = array(
"@type" => "ListItem",
"position" => $position++,
"name" => $clean_title($post_type->labels->name),
"item" => get_post_type_archive_link($post_type->name)
);
// Категории (включая иерархические)
} elseif (is_category()) {
$category = get_queried_object();
// Родительские категории
$parent_cats = array();
$parent_id = $category->parent;
while ($parent_id) {
$parent_cat = get_category($parent_id);
array_unshift($parent_cats, $parent_cat);
$parent_id = $parent_cat->parent;
}
foreach ($parent_cats as $parent_cat) {
$breadcrumbs[] = array(
"@type" => "ListItem",
"position" => $position++,
"name" => $clean_title($parent_cat->name),
"item" => get_category_link($parent_cat->term_id)
);
}
$breadcrumbs[] = array(
"@type" => "ListItem",
"position" => $position++,
"name" => $clean_title($category->name),
"item" => get_category_link($category->term_id)
);
// Теги
} elseif (is_tag()) {
$tag = get_queried_object();
$breadcrumbs[] = array(
"@type" => "ListItem",
"position" => $position++,
"name" => $clean_title($tag->name),
"item" => get_tag_link($tag->term_id)
);
// Кастомные таксономии
}
// elseif (is_tax()) {
// $term = get_queried_object();
// $taxonomy = $term->taxonomy;
//
// // Архив типа поста для этой таксономии
// $taxonomy_obj = get_taxonomy($taxonomy);
// if (!empty($taxonomy_obj->object_type)) {
// $post_type = $taxonomy_obj->object_type[0];
// $post_type_obj = get_post_type_object($post_type);
//
// if ($post_type_obj && $post_type_obj->has_archive) {
// $archive_link = get_post_type_archive_link($post_type);
// if ($archive_link) {
// $breadcrumbs[] = array(
// "@type" => "ListItem",
// "position" => $position++,
// "name" => $clean_title($post_type_obj->labels->name),
// "item" => $archive_link
// );
// }
// }
// }
//
// // Родительские термины для иерархических таксономий
// if (is_taxonomy_hierarchical($taxonomy)) {
// $parent_terms = array();
// $parent_id = $term->parent;
// while ($parent_id) {
// $parent_term = get_term($parent_id, $taxonomy);
// array_unshift($parent_terms, $parent_term);
// $parent_id = $parent_term->parent;
// }
//
// foreach ($parent_terms as $parent_term) {
// $breadcrumbs[] = array(
// "@type" => "ListItem",
// "position" => $position++,
// "name" => $clean_title($parent_term->name),
// /* "item" => get_term_link($parent_term)*/ // TODO: Ссылки на таксономии отключены
// "item" => $archive_link
// );
// }
// }
//
// $breadcrumbs[] = array(
// "@type" => "ListItem",
// "position" => $position++,
// "name" => $clean_title($term->name),
// "item" => get_term_link($term)
// );
//
// // Отдельные посты
// }
elseif (is_single()) {
global $post;
$post_type = get_post_type($post);
if ($post_type === 'post') {
// Обычные посты - категории
$categories = get_the_category();
if (!empty($categories)) {
$main_category = $categories[0];
// Родительские категории
$parent_cats = array();
$parent_id = $main_category->parent;
while ($parent_id) {
$parent_cat = get_category($parent_id);
array_unshift($parent_cats, $parent_cat);
$parent_id = $parent_cat->parent;
}
foreach ($parent_cats as $parent_cat) {
$breadcrumbs[] = array(
"@type" => "ListItem",
"position" => $position++,
"name" => $clean_title($parent_cat->name),
"item" => get_category_link($parent_cat->term_id)
);
}
$breadcrumbs[] = array(
"@type" => "ListItem",
"position" => $position++,
"name" => $clean_title($main_category->name),
"item" => get_category_link($main_category->term_id)
);
}
} else {
// Кастомные типы постов
$post_type_obj = get_post_type_object($post_type);
// Архив типа поста
if ($post_type_obj && $post_type_obj->has_archive) {
$archive_link = get_post_type_archive_link($post_type);
if ($archive_link) {
$breadcrumbs[] = array(
"@type" => "ListItem",
"position" => $position++,
"name" => $clean_title($post_type_obj->labels->name),
"item" => $archive_link
);
}
}
// Основная таксономия для кастомного типа
$taxonomies = get_object_taxonomies($post_type, 'objects');
$main_taxonomy = null;
// Ищем основную таксономию (hierarchical в приоритете)
foreach ($taxonomies as $taxonomy) {
if ($taxonomy->public && !in_array($taxonomy->name, array('post_tag', 'post_format'))) {
if ($taxonomy->hierarchical) {
$main_taxonomy = $taxonomy->name;
break;
} elseif (!$main_taxonomy) {
$main_taxonomy = $taxonomy->name;
}
}
}
// if ($main_taxonomy) {
// $terms = get_the_terms($post, $main_taxonomy);
// if (!empty($terms) && !is_wp_error($terms)) {
// $main_term = $terms[0];
//
// // Родительские термины для иерархических таксономий
// if (is_taxonomy_hierarchical($main_taxonomy)) {
// $parent_terms = array();
// $parent_id = $main_term->parent;
// while ($parent_id) {
// $parent_term = get_term($parent_id, $main_taxonomy);
// array_unshift($parent_terms, $parent_term);
// $parent_id = $parent_term->parent;
// }
//
// foreach ($parent_terms as $parent_term) {
// $breadcrumbs[] = array(
// "@type" => "ListItem",
// "position" => $position++,
// "name" => $clean_title($parent_term->name),
// /* "item" => get_term_link($parent_term)*/
// "item" => $archive_link
// );
// }
// }
//
// $breadcrumbs[] = array(
// "@type" => "ListItem",
// "position" => $position++,
// "name" => $clean_title($main_term->name),
// "item" => get_term_link($main_term)
// );
// }
// }
}
// Текущий пост
$breadcrumbs[] = array(
"@type" => "ListItem",
"position" => $position++,
"name" => $clean_title(get_the_title()),
"item" => get_permalink()
);
// Страницы
} elseif (is_page()) {
global $post;
$ancestors = get_post_ancestors($post);
$ancestors = array_reverse($ancestors);
foreach ($ancestors as $ancestor_id) {
$breadcrumbs[] = array(
"@type" => "ListItem",
"position" => $position++,
"name" => $clean_title(get_the_title($ancestor_id)),
"item" => get_permalink($ancestor_id)
);
}
$breadcrumbs[] = array(
"@type" => "ListItem",
"position" => $position++,
"name" => $clean_title(get_the_title()),
"item" => get_permalink()
);
// Авторы
} elseif (is_author()) {
$author = get_queried_object();
$breadcrumbs[] = array(
"@type" => "ListItem",
"position" => $position++,
"name" => $texts['author_prefix'] . ' ' . $author->display_name,
"item" => get_author_posts_url($author->ID)
);
// Поиск
} elseif (is_search()) {
$breadcrumbs[] = array(
"@type" => "ListItem",
"position" => $position++,
"name" => $texts['search_prefix'] . ' ' . get_search_query(),
"item" => home_url('?s=' . urlencode(get_search_query()))
);
// 404
} elseif (is_404()) {
$breadcrumbs[] = array(
"@type" => "ListItem",
"position" => $position++,
"name" => $texts['not_found'],
"item" => home_url($_SERVER['REQUEST_URI'])
);
}
// Объединяем главную страницу с остальными крошками для JSON-LD
$all_breadcrumbs = array_merge(array($home_breadcrumb), $breadcrumbs);
if (empty($all_breadcrumbs)) {
return false;
}
$yandex_schema = array(
"@context" => "https://schema.org",
"@type" => "BreadcrumbList",
"name" => "Навигация по разделам сайта",
"description" => "Путь навигации к текущей странице",
"areaServed" => array(
"@type" => "Country",
"name" => $current_region
),
"itemListElement" => $all_breadcrumbs
);
?>