Created
December 15, 2020 10:59
-
-
Save nfsarmento/4fc60ba1b19c874cc879726432d9e570 to your computer and use it in GitHub Desktop.
WordPress Menu add class to submenu.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_filter( 'wp_nav_menu_objects', 'ns_add_menu_parent_class' ); | |
| function ns_add_menu_parent_class( $items ) { | |
| $parents = array(); | |
| foreach ( $items as $item ) { | |
| //Check if the item is a parent item | |
| if ( $item->menu_item_parent && $item->menu_item_parent > 0 ) { | |
| $parents[] = $item->menu_item_parent; | |
| } | |
| } | |
| foreach ( $items as $item ) { | |
| if ( in_array( $item->ID, $parents ) ) { | |
| //Add "menu-parent-item" class to parents | |
| $item->classes[] = 'menu-parent-item-submenu'; | |
| } | |
| } | |
| return $items; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment