-
-
Save wpspeak/637a1aad1e487b41260b to your computer and use it in GitHub Desktop.
How to Add Custom Classes to First and Last Items in WordPress Menu
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
<?php | |
add_filter( 'wp_nav_menu_objects', 'afn_custom_menu_class' ); | |
/** | |
* Filters the first and last nav menu objects in your menus | |
* to add custom classes. | |
* | |
* @since 1.0.0 | |
* | |
* @param object $objects An array of nav menu objects | |
* @return object $objects Amended array of nav menu objects with new class | |
*/ | |
function afn_custom_menu_class( $objects ) { | |
// Add "first-menu-item" class to the first menu object | |
$objects[1]->classes[] = 'first-menu-item'; | |
// Add "last-menu-item" class to the last menu object | |
$objects[count( $objects )]->classes[] = 'last-menu-item'; | |
// Return the menu objects | |
return $objects; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment