Created
March 8, 2014 13:34
-
-
Save illucent/9430689 to your computer and use it in GitHub Desktop.
first and last wordpress menu item
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
| function add_menu_parent_class( $items ) { | |
| // GET PARENT ITEMS | |
| $parents = array(); | |
| foreach ( $items as $item ) { | |
| if ( $item->menu_item_parent && $item->menu_item_parent > 0 ) { | |
| $parents[] = $item->menu_item_parent; | |
| } | |
| } | |
| // COUNT PARENT ITEMS | |
| $count = 0; | |
| foreach ( $items as $item ) { | |
| if (in_array( $item->ID, $parents )) { | |
| $count++; | |
| } | |
| } | |
| // ADD CLASSES TO FIRST AND LAST PARENT LI TAGS | |
| $i = 1; | |
| foreach ( $items as $item ) { | |
| if ( in_array( $item->ID, $parents ) ) { | |
| if ($i == 0) { | |
| $item->classes[] = 'first-menu-parent-item'; | |
| } | |
| if ($i == $count) { | |
| $item->classes[] = 'last-menu-parent-item'; | |
| } | |
| $i++; | |
| } | |
| } | |
| return $items; | |
| } | |
| add_filter( 'wp_nav_menu_objects', 'add_menu_parent_class' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment