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.
 
 
 
 
cosmopet-architecture/wp-content/themes/cosmopet/temp-functions/menu-logic.php

118 lines
4.9 KiB

<?php
function register_custom_menus() {
add_theme_support('menus');
register_nav_menus([
'main-menu' => 'Основное меню',
'cats-menu' => 'Меню для кошек',
'dogs-menu' => 'Меню для собак',
]);
}
add_action('after_setup_theme', 'register_custom_menus');
class Custom_Walker_Nav_Menu extends Walker_Nav_Menu {
// Открытие уровня подменю
function start_lvl( &$output, $depth = 0, $args = null ) {
$output .= '<ul class="header-pc-menu__list">';
}
// Закрытие уровня подменю
function end_lvl( &$output, $depth = 0, $args = null ) {
$output .= '</ul>';
}
// Открытие элемента меню
function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
// Для родительского элемента меню
if ($depth === 0) {
$output .= '<div class="header-pc-menu__item">';
$output .= '<a href="' . esc_url($item->url) . '" class="header-pc-menu__title">' . esc_html($item->title) . '</a>';
} else {
// Для дочерних элементов
$output .= '<li class="header-pc-menu__list-li">';
$output .= '<a href="' . esc_url($item->url) . '">' . esc_html($item->title) . '</a>';
}
}
// Закрытие элемента меню
function end_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
// Закрытие для родительского элемента
if ($depth === 0) {
$output .= '</div>'; // Закрытие .header-pc-menu__item
} else {
// Закрытие для дочернего элемента
$output .= '</li>';
}
}
}
function get_custom_menu($location) {
// Используем wp_nav_menu с нужными параметрами
ob_start(); // Начинаем буферизацию вывода
wp_nav_menu(array(
'theme_location' => $location,
'menu_class' => '', // Не добавляем класс к ul
'container' => false, // Без контейнера
'depth' => 2, // Уровень вложенности
'walker' => new Custom_Walker_Nav_Menu(), // Используем кастомный walker
));
$menu_html = ob_get_clean(); // Получаем HTML меню
return $menu_html;
}
class Custom_Header_Phone_Menu_Walker extends Walker_Nav_Menu {
// Открытие подменю (если есть)
function start_lvl( &$output, $depth = 0, $args = null ) {
$output .= '<ul class="header-phone-menu__list">';
}
// Закрытие подменю
function end_lvl( &$output, $depth = 0, $args = null ) {
$output .= '</ul>';
}
// Открытие элемента меню
function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
if ($depth === 0) {
// Для родительского элемента
$output .= '<a href="' . esc_url($item->url) . '" class="header-phone-menu__category">' . esc_html($item->title) . '</a>';
} else {
// Для дочерних элементов
$output .= '<li class="header-phone-menu__list-item">';
$output .= '<a href="' . esc_url($item->url) . '">' . esc_html($item->title) . '</a>';
$output .= '</li>';
}
}
// Закрытие элемента меню
function end_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
// Закрытие элемента на первом уровне (родительский элемент)
if ($depth === 0) {
// Пустое завершение для родительского элемента
} else {
// Закрытие для дочернего элемента
$output .= '</li>';
}
}
}
// Функция для вызова меню и передачи HTML в Twig
function get_custom_phone_menu($location = 'phone_menu') {
ob_start(); // Начинаем буферизацию вывода
wp_nav_menu(array(
'theme_location' => $location, // Указываем местоположение меню
'menu_class' => '', // Не добавляем класс к ul, он будет добавлен кастомно в walker
'container' => false, // Без контейнера
'depth' => 2, // Уровень вложенности (для отображения подменю)
'walker' => new Custom_Header_Phone_Menu_Walker(), // Используем кастомный walker
));
$menu_html = ob_get_clean(); // Получаем HTML меню
return $menu_html;
}