Deprecated function: The each() function is deprecated. This message will be suppressed on further calls in _menu_load_objects() (line 579 of /home/opgnylyd/public_html/includes/menu.inc).

Add a unique class for every menu item

31 Oct 2016
Section: 
Tags: 

Description

Adding a unique class for each menu item for the Drupal 7 themes, using the function in the template file. It may be necessary, for example, to the designation of each menu item to various color, etc.

Solution

Override function theme_menu_link(array $ variables) in your theme file. YOUR_THEME - is the name of your theme, and the class will be added for the li tag, and will look like - menu-[unique ID].

  1. function YOUR_THEME_menu_link(array $variables) {
  2.         $element = $variables['element'];
  3.         $sub_menu = '';
  4.         $element['#attributes']['class'][] = 'menu-' . $element['#original_link']['mlid'];
  5.         if (
  6.                 $element['#below']) {
  7.                         $sub_menu = drupal_render($element['#below']);
  8.                 }
  9.         $output = l($element['#title'], $element['#href'], $element['#localized_options']);
  10.         return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
  11. }

Comments