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!

Leave a Comment

DON’T MISS OUR GIVEAWAYS!
Subscribe To Newsletter
Subscribe to our newsletter and get all the latest updates offers and giveaways right in your inbox.
Subscribe
Give it a try, you can unsubscribe anytime.
close-link

Join Free Giveaway!

Subscribe to our newsletter to get latest offers and giveaways!
SUBSCRIBE
close-link