N
N
Nikolai Novosad2017-09-11 17:49:39
Drupal
Nikolai Novosad, 2017-09-11 17:49:39

Set class for li and a in menu?

Hello.
I display the menu in the template like this:

print theme('links', array('links' => menu_navigation_links('menu-company'), 'attributes' => array('class'=> array('links', 'footer-nav footer__nav')) ));

The menu is displayed. Now we need to add a class for the 'li' and 'a' tags.
Tried like this in template.php
function mytheme_menu_link(array $variables) {
    $element = $variables['element'];
    $sub_menu = '';

    if ($element['#below']) {
        $sub_menu = drupal_render($element['#below']);
    }
    $element['#attributes']['class'][] = 'footer-nav__item';
    $output = l($element['#title'], $element['#href'], $element['#localized_options']);
    return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

But it didn't. Tried to debug this function, but to no avail. It feels like the function is not working.
Please tell me what could be the issue.
Drupal v7. There are no child elements in the menu.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Alexandrov, 2017-09-12
@nik_neman

menu_attributes not an option?

K
Kirill Shatokhin, 2017-11-01
@bonzik

into template

<?php
$menu = menu_tree('mymenu');
echo render($menu);
?>

in template.php
function mytheme_menu_link(array $variables) {
    $element = $variables['element'];
    $sub_menu = '';

    if ($element['#below']) {
        $sub_menu = drupal_render($element['#below']);
    }
    $element['#attributes']['class'][] = 'footer-nav__item';
    $output = l($element['#title'], $element['#href'], $element['#localized_options']);
    return '<li' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>\n";
}

don't forget to change mymenu and mytheme

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question