Add Classes to First and Last Menu Item in WordPress

Add Custom Menu class to First and Last menu item of WP Nav Menu. The following function will generate separate class for the first and last menu item in WordPress Menu. Thus we can apply custom style to those menu items.

Add the following function in your function.php file

function wpb_first_and_last_menu_class($items) {
$items[1]->classes[] = 'wp_menu_first';
$items[count($items)]->classes[] = 'wp_menu_last';
return $items;
}
add_filter('wp_nav_menu_objects', 'wpb_first_and_last_menu_class');

Here ‘wp_menu_first’ and ‘wp_menu_last’ are the two classes generated for first and last menu item.

You can so easily style those menu item using these classes in your style.css file.

Sharing is caring!

Related Posts

Leave a Comment

shares