4.6.x theme.inc theme_links($links, $delimiter = ' | ')
4.7.x theme.inc theme_links($links, $delimiter = ' | ')
5.x theme.inc theme_links($links, $attributes = array('class' => 'links'))
6.x theme.inc theme_links($links, $attributes = array('class' => 'links'))
7.x theme.inc theme_links($variables)

Returns HTML for a set of links.

Parameters

$links: An associative array of links to be themed. The key for each link is used as its CSS class. Each link should be itself an array, with the following elements:

  • title: The link text.
  • href: The link URL. If omitted, the 'title' is shown as a plain text item in the links list.
  • html: (optional) Whether or not 'title' is HTML. If set, the title will not be passed through check_plain().
  • attributes: (optional) Attributes for the anchor, or for the <span> tag used in its place if no 'href' is supplied.

If the 'href' element is supplied, the entire link array is passed to l() as its $options parameter.

$attributes: An associative array of attributes for the UL containing the list of links.

Return value

A string containing an unordered list of links.

Related topics

13 theme calls to theme_links()
chameleon_comment in themes/chameleon/chameleon.theme
chameleon_node in themes/chameleon/chameleon.theme
chameleon_page in themes/chameleon/chameleon.theme
forums.tpl.php in modules/forum/forums.tpl.php
forums.tpl.php Default theme implementation to display a forum which may contain forum containers as well as forum topics.
locale_block in modules/locale/locale.module
Implementation of hook_block(). Displays a language switcher. Translation links may be provided by other modules.

... See full list

File

includes/theme.inc, line 1215
The theme system, which controls the output of Drupal.

Code

function theme_links($links, $attributes = array('class' => 'links')) {
  global $language;
  $output = '';

  if (count($links) > 0) {
    $output = '<ul' . drupal_attributes($attributes) . '>';

    $num_links = count($links);
    $i = 1;

    foreach ($links as $key => $link) {
      $class = $key;

      // Add first, last and active classes to the list of links to help out themers.
      if ($i == 1) {
        $class .= ' first';
      }
      if ($i == $num_links) {
        $class .= ' last';
      }
      if (isset($link['href']) && ($link['href'] == $_GET['q'] || ($link['href'] == '<front>' && drupal_is_front_page()))
         && (empty($link['language']) || $link['language']->language == $language->language)) {
        $class .= ' active';
      }
      $output .= '<li' . drupal_attributes(array('class' => $class)) . '>';

      if (isset($link['href'])) {
        // Pass in $link as $options, they share the same keys.
        $output .= l($link['title'], $link['href'], $link);
      }
      else if (!empty($link['title'])) {
        // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
        if (empty($link['html'])) {
          $link['title'] = check_plain($link['title']);
        }
        $span_attributes = '';
        if (isset($link['attributes'])) {
          $span_attributes = drupal_attributes($link['attributes']);
        }
        $output .= '<span' . $span_attributes . '>' . $link['title'] . '</span>';
      }

      $i++;
      $output .= "</li>\n";
    }

    $output .= '</ul>';
  }

  return $output;
}

Comments

davidwhthomas’s picture

Note, this theme function is the one to override for theming primary links.

pwaterz’s picture

You use this function to override the the output of anywhere the theme_links function is being used. So not just primary links, could also be secondary links as well. Lullabot has a good video on this http://www.lullabot.com/videos/using-secondary-menus.

massimoi’s picture

In case you need to allow HTML in link titles, simply change this

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

with this:

if (isset($link['href'])) {
		$link['html'] = true;
        // Pass in $link as $options, they share the same keys.
        $output .= l($link['title'], $link['href'], $link);
}
Blackice2999’s picture

please dont suggest other users to hack the core... core hacking will be prevent you from updates. You can create a theme override in your template.php and modify the code at this point.

best regards
Dennis

gagarine’s picture

You don't never need to break the core. Use the option 'html' => TRUE.

$links = array();
$links[my_link] = array(
      'title' => '

un BIG title
 with html

' 'href' => 'page/path', 'html' => TRUE, ); $links[my_link_separator] = array( 'title' => '
' 'html' => TRUE, ); $output = theme_links($links,array('id' => 'my_links'));

If you want pass more specific html use theme_item_list

NancyDru’s picture

While PHP may let you get away with, this, you are still producing error messages. $links[my_link] needs apostrophes around that array key ($links['my_link']).

xalexas’s picture

Can someone explain how to use this piece of function? I would like to print non links in menu, but can't figure out how to make this work. How to trigger this code?

else if (!empty($link['title'])) {
        // Some links are actually not links, but we wrap these in <span> for adding title and class attributes
        if (empty($link['html'])) {
          $link['title'] = check_plain($link['title']);
        }
        $span_attributes = '';
        if (isset($link['attributes'])) {
          $span_attributes = drupal_attributes($link['attributes']);
        }
        $output .= '<span' . $span_attributes . '>' . $link['title'] . '</span>';
      }
NancyDru’s picture

Theme_links is NOT a menu function. I'm not even sure menu.inc uses this theme function.

This kind of question belongs in the forums.