Task 6875 | Перенес логику меню в отдельный модуль

pull/36/head
parent e266a8ece7
commit c37295ba6a
  1. 84
      wp-content/themes/cosmopet/modules/header/module-controller.php
  2. 62
      wp-content/themes/cosmopet/temp-functions/menu-logic.php
  3. 5
      wp-content/themes/cosmopet/templates/header.twig

@ -5,88 +5,6 @@ function header_enqueue_scripts() {
} }
add_action('wp_enqueue_scripts', 'header_enqueue_scripts'); add_action('wp_enqueue_scripts', 'header_enqueue_scripts');
function is_home_page_posts_archive() { include_module('menu');
// 1. Если главная страница - это страница записей
if (get_option('show_on_front') == 'posts') {
return true;
}
// 2. Если главная - статическая страница, но выводит архив
if (get_option('show_on_front') == 'page') {
$front_page_id = get_option('page_on_front');
// Проверяем, является ли это архивом
return is_post_type_archive_view($front_page_id);
}
return false;
}
// Дополнительная функция для проверки шаблона архива
function is_post_type_archive_view($page_id) {
$template = get_page_template_slug($page_id);
return strpos($template, 'archive') !== false ||
strpos($template, 'blog') !== false;
}
add_filter('timber/context', function($context) {
// Проверяем существование страниц
$about_page = Timber::get_post([
'post_type' => 'page',
'name' => 'o-kompanii',
'limit' => 1
]);
$production_page = Timber::get_post([
'post_type' => 'page',
'name' => 'proizvodstvo', // Обратите внимание на возможную опечатку в slug
'limit' => 1
]);
// Устанавливаем URL для страницы "О компании"
if ($about_page) {
$context['about_url'] = '/o-kompanii/';
$context['about_url_en'] = '/en/about-us/';
}
else if (SITE_DOMAIN == 'AE'){
$context['about_url_en'] = '/about-us/';
}
else {
$context['about_url'] = 'https://cosmopet.shop/chto-takoe-entoprotein/';
$context['about_url_en'] = 'https://cosmopet.shop/en/about-2/';
}
// Устанавливаем URL для страницы "Производство"
if ($production_page) {
$context['production_url'] = '/proizvodstvo/';
$context['production_url_en'] = '/en/production/';
}
else if (SITE_DOMAIN == 'AE'){
$context['production_url_en'] = '/production/';
}
else {
$context['production_url'] = 'https://cosmopet.shop/proizvodstvo/';
$context['production_url_en'] = 'https://cosmopet.shop/en/production/';
}
if (!is_home_page_posts_archive()) {
$context['front_url'] = "/";
$context['front_url_en'] = "/en/main/";
}
else{
$context['front_url'] = "https://cosmopet.shop/";
$context['front_url_en'] = "https://cosmopet.shop/en/main/";
}
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;
});
?> ?>

@ -11,70 +11,8 @@ function register_custom_menus() {
} }
add_action('after_setup_theme', 'register_custom_menus'); 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'); add_filter('timber/context', 'add_to_context');
/**
* Global Timber context.
*
* @param array $context Global context variables.
*/
function add_to_context($context) function add_to_context($context)
{ {
$context['main_menu'] = Timber::get_menu('main-menu'); $context['main_menu'] = Timber::get_menu('main-menu');

@ -1,8 +1,3 @@
{# Получаем ID страницы блога из настроек #}
{% set posts_page_id = function('get_option', 'page_for_posts') %}
<header class="header {{headerClass}}"> <header class="header {{headerClass}}">
<div class="header__menu-block"> <div class="header__menu-block">
<div class="header__pc-menu"> <div class="header__pc-menu">

Loading…
Cancel
Save