4

In Drupal 7, I'm trying to add a <span> tag inside the <a> tag of an item in the Main Menu, but it only works for the 'Navigation' menu which is a block, however, I am trying render it within page.tpl.php

I've been searching this up for a while now and can't seem to get it to work. I was attempting to follow the information from How do I insert a <span> </span> tag inside the <a> </a> tag on my main menu

Here's a snippet from page.tpl.php

  <nav id="main-menu" class="navigation span8">
    <?php print theme('links__system_main_menu', array(
      'links' => $main_menu,
      'attributes' => array(
        'id' => 'main-menu-links',
        'class' => array('links', 'clearfix'),
      ),
      'heading' => array(
        'text' => t('Main menu'),
        'level' => 'h2',
        'class' => array('element-invisible'),
      ),
     )); ?>
  </nav> <!-- /#main-menu -->

and from template.php

function THEMENAME_menu_link(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';

//$element['#attributes']['class'][] = 'menu-' . $element['#original_link']['mlid'];

$get_title= $element['#title'];
$element['#title']='<span>' . $get_title .' </span>';

$element['#localized_options'] += array(
    'attributes' => array(), 
    'html' => TRUE,
  );

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

2 Answers 2

6

The function you need to override is theme_links() and to do that just for the main menu you can use mytheme_links__system_main_menu as the function name. To add span tags you can then modify the part that starts with if (isset($link['href'])):

if (isset($link['href'])) {
  // Pass in $link as $options, they share the same keys.
  $link['html'] = TRUE;
  $output .= l('<span>'. check_plain($link['title']) .'</span>', $link['href'], $link);
}
0
0

You could try THEME_menu_link__main_menu(array $variables)

function THEMENAME_menu_link__main_menu(array $variables) {
  $element = $variables['element'];
  $sub_menu = '';

//$element['#attributes']['class'][] = 'menu-' . $element['#original_link']['mlid'];

$get_title= $element['#title'];
$element['#title']='<span>' . $get_title .' </span>';

$element['#localized_options'] += array(
    'attributes' => array(), 
    'html' => TRUE,
  );

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.