WordPress.org

Make WordPress Core

Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27616 closed enhancement (duplicate)

Add first-menu-item and last-menu-item classes

Reported by: core_2_duo Owned by:
Milestone: Priority: normal
Severity: trivial Version: 3.8.1
Component: Menus Keywords: 2nd-opinion close
Focuses: Cc:

Description

Add first-menu-item and last-menu-item classes to nav menus items, including all child menus. It's may be useful for CSS when you have to build site with IE6-7 support, because you often need to set borders or colors of the first or of the last elements.

Pull request & code are here: https://github.com/WordPress/WordPress/pull/78

Attachments (1)

nav-menu-template.php (22.8 KB) - added by core_2_duo 8 years ago.
Updated nav-menu-template.php

Download all attachments as: .zip

Change History (6)

@core_2_duo
8 years ago

Updated nav-menu-template.php

#1 follow-up: @fgirardey
8 years ago

  • Keywords 2nd-opinion added

If you really want to support IE6-7 use this filter instead nav_menu_css_class :)

#2 in reply to: ↑ 1 @core_2_duo
8 years ago

Replying to fgirardey:

If you really want to support IE6-7 use this filter instead nav_menu_css_class :)

I use this filter now

function add_first_and_last($items) {
    $parents = array();
    foreach ($items as $key => $item) {
        if (! array_key_exists($item->menu_item_parent, $parents)) {
            $item->classes []= 'first-menu-item';
        } else {
            array_pop($items[$parents[$item->menu_item_parent]]->classes);
        }
        $parents[$item->menu_item_parent] = $key;
        $item->classes []= 'last-menu-item';
    }
    return $items;
}
add_filter('wp_nav_menu_objects', 'add_first_and_last');

It is similar to code, that I've added in nav-menu-template.php.

But I guess, you are right. It's kinda specific feature and it can be solved through filters, when you need it.

#3 @core_2_duo
8 years ago

  • Severity changed from normal to trivial

#4 @fgirardey
8 years ago

  • Keywords close added
  • Resolution set to invalid
  • Status changed from new to closed

#5 @helen
8 years ago

  • Milestone Awaiting Review deleted
  • Resolution changed from invalid to duplicate

Duplicate of #14250.

Note: See TracTickets for help on using tickets.