From e3902119412971ab85ebdeb31b95b4e8a0de8632 Mon Sep 17 00:00:00 2001 From: Nico Mollet Date: Wed, 4 Oct 2017 17:55:31 +0200 Subject: [PATCH] * **Tweak** - Menu .active class for current item --- inc/front/menu.php | 550 +++++++++++++++++++++++---------------------- 1 file changed, 286 insertions(+), 264 deletions(-) diff --git a/inc/front/menu.php b/inc/front/menu.php index bba06ed..9e1866c 100644 --- a/inc/front/menu.php +++ b/inc/front/menu.php @@ -8,59 +8,81 @@ /* * Maybe make current for post type archives or top categories */ +/** + * + * @param $items + * + * @return mixed + */ function stormbringer_maybe_make_current($items) { - foreach ($items as $item) { - if ('cpt-archive' != $item->object) { - continue; - } - - $post_type = $item->type; - if ( ! is_post_type_archive($post_type) && ! is_singular($post_type)) { - continue; - } - - //Make item current - $item->current = true; - $item->classes[] = 'current-menu-item'; - - //Get menu item's ancestors: - $_anc_id = (int)$item->db_id; - $active_ancestor_item_ids = array(); - - while (($_anc_id = get_post_meta($_anc_id, '_menu_item_menu_item_parent', true)) - && - ! in_array($_anc_id, $active_ancestor_item_ids)) { - $active_ancestor_item_ids[] = $_anc_id; - } - - //Loop through ancestors and give them 'ancestor' or 'parent' class - foreach ($items as $key => $parent_item) { - $classes = (array)$parent_item->classes; - - //If menu item is the parent - if ($parent_item->db_id == $item->menu_item_parent) { - $classes[] = 'current-menu-parent'; - $items[$key]->current_item_parent = true; - } - - //If menu item is an ancestor - if (in_array(intval($parent_item->db_id), $active_ancestor_item_ids)) { - $classes[] = 'current-menu-ancestor'; - $items[$key]->current_item_ancestor = true; - } - - $items[$key]->classes = array_unique($classes); - } - - } - - return $items; + foreach ($items as $item) { + if ('cpt-archive' != $item->object) { + continue; + } + + $post_type = $item->type; + if ( ! is_post_type_archive($post_type) && ! is_singular($post_type)) { + continue; + } + + //Make item current + $item->current = true; + $item->classes[] = 'current-menu-item'; + + //Get menu item's ancestors: + $_anc_id = (int)$item->db_id; + $active_ancestor_item_ids = array(); + + while (($_anc_id = get_post_meta($_anc_id, '_menu_item_menu_item_parent', true)) + && + ! in_array($_anc_id, $active_ancestor_item_ids)) { + $active_ancestor_item_ids[] = $_anc_id; + } + + //Loop through ancestors and give them 'ancestor' or 'parent' class + foreach ($items as $key => $parent_item) { + $classes = (array)$parent_item->classes; + + //If menu item is the parent + if ($parent_item->db_id == $item->menu_item_parent) { + $classes[] = 'current-menu-parent'; + $items[$key]->current_item_parent = true; + } + + //If menu item is an ancestor + if (in_array(intval($parent_item->db_id), $active_ancestor_item_ids)) { + $classes[] = 'current-menu-ancestor'; + $items[$key]->current_item_ancestor = true; + } + + $items[$key]->classes = array_unique($classes); + } + + } + + return $items; } - add_filter('wp_nav_menu_objects', 'stormbringer_maybe_make_current'); +/** + * Menus active class + * + * @param $classes + * @param $item + * + * @return array + */ +function stormbringer_nav_menu_css_class ($classes, $item) { + if (in_array('current-menu-item', $classes) ){ + $classes[] = 'active '; + } + return $classes; +} +add_filter('nav_menu_css_class' , 'stormbringer_nav_menu_css_class' , 10 , 2); + + /** * A custom WordPress nav walker class to implement the Bootstrap 3 navigation style in a custom theme using the WordPress built in menu manager. * @@ -69,221 +91,221 @@ function stormbringer_maybe_make_current($items) class Stormbringer_Navbar_Nav_Walker extends Walker_Nav_Menu { - /** - * @see Walker::start_lvl() - * @since 3.0.0 - * - * @param string $output Passed by reference. Used to append additional content. - * @param int $depth Depth of page. Used for padding. - */ - public function start_lvl(&$output, $depth = 0, $args = array()) - { - $indent = str_repeat("\t", $depth); - $output .= "\n$indent'; - - if ($container) { - $fb_output .= ''; - } - - echo $fb_output; - } - } + /** + * @see Walker::start_lvl() + * @since 3.0.0 + * + * @param string $output Passed by reference. Used to append additional content. + * @param int $depth Depth of page. Used for padding. + */ + public function start_lvl(&$output, $depth = 0, $args = array()) + { + $indent = str_repeat("\t", $depth); + $output .= "\n$indent'; + + if ($container) { + $fb_output .= ''; + } + + echo $fb_output; + } + } }