WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

#20347 closed enhancement (duplicate)

Extra menu classes ( First, Last )

Reported by: veke Owned by: vekexasia@…
Milestone: Priority: normal
Severity: minor Version: 3.3.1
Component: Menus Keywords: close
Focuses: Cc:

Description

It would be nice to add, only to the first and the last item, an extra class to the current ( very useful ) list of menu classes.

Ex: if the menu is composed as following:
# Home
# Second Page
# Third Page
# Last page.

than :

Home will also have the class $menuClass-first
Last page will also have the class $menuClass-last

So far i solved this "issue" hooking to the filter wp_nav_menu_objects with the following code:

static function addUsefulClassesToMenusElements($sorted_menu_items, $args) {
		$count = 0;
		$menuClass = $args->menu_class;

		foreach ($sorted_menu_items as $k=>$item) {
			if ($count == 0) {
				$item->classes[] = $menuClass.'-first';
			}
			if ($count == count($sorted_menu_items)-1) {
				$item->classes[] = $menuClass.'-last';
			}
			$count++;
		}
		return $sorted_menu_items;

	}

Change History (4)

comment:2 veke2 years ago

:(

comment:3 helenyhou2 years ago

  • Keywords close added; has-patch removed

:first-child and/or :last-child should work in modern browsers, in any case. I believe :first-child is IE7+, :last-child is IE9+.

comment:4 ocean902 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
  • Type changed from feature request to enhancement

#9011

helenyhou, that's right.

Note: See TracTickets for help on using tickets.