Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Filters the first and last nav menu objects in your menus to add custom classes.
- * @param object $objects An array of nav menu objects
- * @param object $args Nav menu object args
- * @return object $objects Amended array of nav menu objects with new class
- * ref link: http://thomasgriffinmedia.com/blog/2012/12/how-to-add-custom-classes-first-last-menu-items-wordpress/
- * Augmented Griffin's code with while and if loops to assign distinct css class to each menu item by Virtually Marj http://virtuallymarj.com
- * While my project only required classes for the primary menu, this code could be embellished to address any menu location or menu in your theme
- */
- add_filter( 'wp_nav_menu_objects', 'vm_filter_menu_class', 10, 2 );
- function vm_filter_menu_class( $objects, $args ) {
- if ( isset( $args->theme_location ) )
- if ( 'primary' !== $args->theme_location ) // Only apply the classes to the primary navigation menu
- return $objects;
- $navitems = wp_get_nav_menu_object( 'main' ); // this is the custom menu name
- $navcount = $navitems->count;
- $i=1;
- while ( $i <> $navcount ) {
- // Add distinct class to menu objects
- $objects[$i]->classes[] = 'images-menu-item-' . $i;
- $i++;
- }
- // Add "last-menu-item" class to the last menu object
- if ( $i == $navcount ) {
- $objects[count( $objects )]->classes[] = 'last-menu-item';
- }
- // Return the menu objects
- return $objects;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement