• Resolved femalemoustache

    (@femalemoustache)


    Hello!
    I use wp_nav_menu() to show site menu. How can I add the “last” class to the li element of the last menu item by using “nav_menu_css_class” filter?

Viewing 4 replies - 1 through 4 (of 4 total)
  • You don’t need to add a separate class.
    You can select the last li like this

    li:last-child {}

    Thread Starter femalemoustache

    (@femalemoustache)

    I know about li:last-child and it’s jQuery variant. But can i do it via “nav_menu_css_class” filter?

    I’ve done this before using the following code. Just add it to your functions.php file. It doesn’t use the filter that you mentioned but it does the trick.

    function add_menu_item_last_class( $items ){
       $items[count($items)]->classes[] = 'last';
       return $items;
    }
    
    add_filter('wp_nav_menu_objects', 'add_menu_item_last_class');

    Another way would be to turn on CSS Classes from Screen Options on the Appearance->Menus page and then add the class manually to the required menu item.

    Thread Starter femalemoustache

    (@femalemoustache)

    Thank you, martinjohnharvey! This method doens’t use that filter, but uses the logic i wanted.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘how to add the "last" class to the last menu item?’ is closed to new replies.