From db11843fca09705ca449c8afa79091dfd6512c49 Mon Sep 17 00:00:00 2001 From: maksim Date: Mon, 23 Jun 2025 12:46:19 +0300 Subject: [PATCH] =?UTF-8?q?Task=206875=20|=20=D0=BF=D0=B5=D1=80=D0=B5?= =?UTF-8?q?=D1=81=D0=BE=D0=B1=D1=80=D0=B0=D0=BB=20=D0=BC=D0=B5=D0=BD=D1=8E?= =?UTF-8?q?=20=D0=B8=20=D0=B5=D0=B3=D0=BE=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4?= =?UTF-8?q?=20=D1=81=20=D0=BF=D0=BE=D0=BC=D0=BE=D1=89=D1=8C=D1=8E=20WP=5Fn?= =?UTF-8?q?av=5Fmenu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/header/module-controller.php | 5 + .../cosmopet/temp-functions/menu-logic.php | 109 +++++++++- .../themes/cosmopet/templates/header.twig | 194 +++--------------- 3 files changed, 142 insertions(+), 166 deletions(-) diff --git a/wp-content/themes/cosmopet/modules/header/module-controller.php b/wp-content/themes/cosmopet/modules/header/module-controller.php index 09f52bd..b59c312 100644 --- a/wp-content/themes/cosmopet/modules/header/module-controller.php +++ b/wp-content/themes/cosmopet/modules/header/module-controller.php @@ -79,6 +79,11 @@ add_filter('timber/context', function($context) { if (SITE_DOMAIN == 'AE'){ $context['front_url_en'] = "/"; } + + $current_language = pll_current_language(); // Получаем текущий язык + $menu_items = wp_get_nav_menu_items('main_menu_' . $current_language); // Получаем элементы меню для текущего языка + + $context['menu_items'] = $menu_items; return $context; }); diff --git a/wp-content/themes/cosmopet/temp-functions/menu-logic.php b/wp-content/themes/cosmopet/temp-functions/menu-logic.php index ebac216..72b628a 100644 --- a/wp-content/themes/cosmopet/temp-functions/menu-logic.php +++ b/wp-content/themes/cosmopet/temp-functions/menu-logic.php @@ -8,4 +8,111 @@ function register_custom_menus() { 'dogs-menu' => 'Меню для собак', ]); } -add_action('after_setup_theme', 'register_custom_menus'); \ No newline at end of file +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 .= ''; + } + + // Открытие элемента меню + function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { + // Для родительского элемента меню + if ($depth === 0) { + $output .= '
'; + $output .= '' . esc_html($item->title) . ''; + } else { + // Для дочерних элементов + $output .= '
  • '; + $output .= '' . esc_html($item->title) . ''; + } + } + + // Закрытие элемента меню + function end_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { + // Закрытие для родительского элемента + if ($depth === 0) { + $output .= '
  • '; // Закрытие .header-pc-menu__item + } else { + // Закрытие для дочернего элемента + $output .= ''; + } + } +} + + +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 .= ''; + } + + // Открытие элемента меню + function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { + if ($depth === 0) { + // Для родительского элемента + $output .= '' . esc_html($item->title) . ''; + } else { + // Для дочерних элементов + $output .= '
  • '; + $output .= '' . esc_html($item->title) . ''; + $output .= '
  • '; + } + } + + // Закрытие элемента меню + function end_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { + // Закрытие элемента на первом уровне (родительский элемент) + if ($depth === 0) { + // Пустое завершение для родительского элемента + } else { + // Закрытие для дочернего элемента + $output .= ''; + } + } +} + + +// Функция для вызова меню и передачи 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; +} \ No newline at end of file diff --git a/wp-content/themes/cosmopet/templates/header.twig b/wp-content/themes/cosmopet/templates/header.twig index 759e912..cb93c1d 100644 --- a/wp-content/themes/cosmopet/templates/header.twig +++ b/wp-content/themes/cosmopet/templates/header.twig @@ -1,71 +1,25 @@ {# Получаем ID страницы блога из настроек #} {% set posts_page_id = function('get_option', 'page_for_posts') %} -{# Получаем URL этой страницы #} -{% if posts_page_id %} -{% set blog_url = function('get_permalink', posts_page_id) %} -{% else %} -{% set blog_url = '/' %} -{% endif %} +
    {% if current_lang == 'ru' %}ГЛАВНАЯ{% elseif current_lang == 'en' %}MAIN{% endif %} + class="header-phone-menu__title header-phone-menu__title--gradient">{% if current_lang == 'ru' %}ГЛАВНАЯ{% elseif current_lang == 'en' %}MAIN{% endif %}
    {% if current_lang == 'ru' %} @@ -74,89 +28,26 @@ ПРОДУКЦИЯ - Кошкам - - Собакам - + {% set cat_m_menu = fn('get_custom_phone_menu', 'cats-menu') %} + {{ cat_m_menu|raw }} + {% set dog_m_menu = fn('get_custom_phone_menu', 'dogs-menu') %} + {{ dog_m_menu|raw }}
    {% elseif current_lang == 'en' %}
    - + BUY - For - cats - - For - dogs - + {% set cat_m_menu = fn('get_custom_phone_menu', 'cats-menu') %} + {{ cat_m_menu|raw }} + {% set dog_m_menu = fn('get_custom_phone_menu', 'dogs-menu') %} + {{ dog_m_menu|raw }}
    {% endif %} @@ -169,7 +60,7 @@ {% if CONSTANTS.DOMAIN != 'AE' %} {% endif %} @@ -201,46 +92,19 @@ -
    {% if site_region == 'ru' %}