I'm in the process of trying to theme my site and I am having some trouble with the primary links. I need to add a < span > inside the < li > element.

I have the following code in my page.tpl.php page:

<div id="menu-top">
  <?php if ($primary_links): ?>
  <?php print theme('menu_links', $primary_links); ?>
  <?php endif; ?>
</div>

which outputs this:

<div id="menu-top">
  <ul>
    <li>link 1</li>
    <li>link 2</li>
    <li>link 3</li>
    <li>link 4</li>
  </ul>
</div>

What I need it to generate is this:

<div id="menu-top">
  <ul>
    <li><span>link 1</span></li>
    <li><span>link 2</span></li>
    <li><span>link 3</span></li>
    <li><span>link 4</span></li>
  </ul>
</div>

Any ideas? Thanks!

:cD

Comments

canen’s picture

Override the theme_menu_links function in your template.php file.

Hope that helps.

STyL3’s picture

i guess i should have said that i'm new to drupal. Anyone have a step by step process that i could follow?

canen’s picture

In that case the PHPTemplate handbook is the perfect place to start. You can also look at the Zen theme as a start.

Good luck.

d------’s picture

It does not answer this question... sadly...

davemybes’s picture

When you see functions that start with "theme_", you can simply copy the entire function, paste it into template.php (which goes in your theme folder), and change the word theme (in the function name only) to the name of your theme. Then modify the code to do what you want. Like this:

function theme-name-here_menu_links($links) {
  if (!count($links)) {
    return '';
  }
  $level_tmp = explode('-', key($links));
  $level = $level_tmp[0];
  $output = "<ul class=\"links-$level\">\n";
  foreach ($links as $index => $link) {
    $output .= '<li';
    if (stristr($index, 'active')) {
      $output .= ' class="active"';
    }
    $output .= "><span>". l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment']) ."</span></li>\n";
  }
  $output .= '</ul>';

  return $output;
} 

This is the exact code that was linked to in an earlier post, except that I have added the span element to the very long $output line. That's the only possibly tricky bit - finding out where your modifications have to go.
______________________________________________________________________________________________________
Need help? Check the FAQ and the Handbooks first.

______________________________________________________________________________________________________