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

85 lines
3.2 KiB

<?php
function register_custom_menus() {
add_theme_support('menus');
register_nav_menus([
'main-menu' => 'Основное меню',
'cats-menu' => 'Меню для кошек',
'dogs-menu' => 'Меню для собак',
'mobile-menu' => 'Мобильное меню',
]);
}
add_action('after_setup_theme', 'register_custom_menus');
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;
}
add_filter('timber/context', 'add_to_context');
/**
* Global Timber context.
*
* @param array $context Global context variables.
*/
function add_to_context($context)
{
$context['main_menu'] = Timber::get_menu('main-menu');
$context['cat_menu'] = Timber::get_menu('cats-menu');
$context['dog_menu'] = Timber::get_menu('dogs-menu');
$context['mobile_menu'] = Timber::get_menu('mobile-menu');
return $context;
}