A better and simpler approach: function add_first_and_last($items) { $items[1]-> classes[] = 'first-menu-item'; $items[count($items)]-> classes[] = 'last-menu-item'; return $items; } add_filter('wp_nav_menu_objects', 'add_first_and_last'); More Solution Can a WordPress plugin or theme contain a virus? in WordPress plugin development - What is a settings group actually for? in WordPress What plugins are in use on wordpress.com in WordPress Widget code is not working on WordPress.com Hosted Blogs in WordPress How do you remove Link backs on Theme settings page? in WordPress Reffered: https://wordpress.org/