From 51b8db75922fba4f19e555311ec65b3998ea4806 Mon Sep 17 00:00:00 2001 From: Antoine M Date: Fri, 30 Jan 2026 10:18:28 +0100 Subject: [PATCH] REFACTOR Refining primary menu wrapping --- includes/navwalker.php | 29 +++++++++++++++++++++++------ includes/nawalker_fction.php | 5 +---- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/includes/navwalker.php b/includes/navwalker.php index a3cd02e..7ff2069 100644 --- a/includes/navwalker.php +++ b/includes/navwalker.php @@ -6,21 +6,38 @@ function wrap_parent_menu_item_buttons($output, $item, $depth, $args) { + + // #### MENU HOMEGRADE HEADER if ($args->theme_location === "secondary" && in_array('menu-item-has-children', $item->classes, true)) { $output = ''; + return $output; } - if ($args->theme_location === "primary" && in_array('menu-item-has-children', $item->classes, true)) { + if ($args->theme_location === "primary" && in_array('menu-item-has-children', $item->classes, true) && $depth === 0) { $page_icon = get_field('page_icon', $item->object_id) ?? null; $icon = isset($page_icon) && is_array($page_icon) && !empty($page_icon['url']) ? '' : ''; $output = ''; + return $output; } - // write_log($item->classes); - if ($args->theme_location === "primary" && !in_array('menu-item-has-children', $item->classes, true)) { + + + if ($args->theme_location === "primary" && !in_array('menu-item-has-children', $item->classes, true) && $depth === 1) { $page_icon = get_field('page_icon', $item->object_id) ?? null; - $icon = isset($page_icon) && is_array($page_icon) && !empty($page_icon['url']) ? '' : ''; - $output = '' . $icon . $item->title . ''; + $icon_html = isset($page_icon) && is_array($page_icon) && !empty($page_icon['url']) ? '' : ''; + + $page_subtitle = get_field('page_subtitle', $item->object_id) ?? ''; + $subtitle_html = '' . $page_subtitle . ''; + + if ($item->type === 'post_type_archive') { + $icon_html = get_archive_page_icon_html($item->object); + $subtitle_html = get_archive_page_subtitle_html($item->object); + } + + + + $output = '' . $icon_html . ''; + return $output; } return $output; } @@ -69,7 +86,7 @@ function tailpress_nav_menu_add_submenu_class($classes, $args, $depth) add_filter('nav_menu_submenu_css_class', 'tailpress_nav_menu_add_submenu_class', 10, 3); /* ---------------------------------------------------------------- - ##### Inject ACF page_icon into submenu items + ##### Inject ACF page_icon and page_subtitle into submenu items ------------------------------------------------------------------*/ function carhop_add_submenu_item_icon($output, $item, $depth, $args) { diff --git a/includes/nawalker_fction.php b/includes/nawalker_fction.php index 2fa9795..3eb3ccc 100644 --- a/includes/nawalker_fction.php +++ b/includes/nawalker_fction.php @@ -8,12 +8,9 @@ function carhop_wrap_parent_menu_item_buttons($output, $item, $depth, $args) { if ($args->theme_location === "primary") { $page_icon = get_field('page_icon', $item->object_id) ?? null; - // $test = get_field('page_icon', 729); - // write_log($test); if (isset($page_icon) && is_array($page_icon) && !empty($page_icon['url'])) { $icon = ''; - // Insérer l'icône à l'intérieur du lien (seulement la première occurrence) $output = preg_replace('/>/', '>' . $icon, $output, 1); } } @@ -36,7 +33,7 @@ add_filter('walker_nav_menu_start_el', 'carhop_wrap_parent_menu_item_buttons', 1 function carhop_add_page_icon_class($classes, $item) { $page_icon = get_field('page_icon', $item->object_id); - if (isset($page_icon)) { + if (isset($page_icon) || $item->type === 'post_type_archive') { $classes[] = 'has-page-icon'; } return $classes;