In 4.x, it was really easy...

print theme('links', $primary_links, "
"); ...or say... print theme('links', $primary_links, " ** ");

But with 5.x, the primary link code for the theme looks something like this:

print theme('links', $primary_links, array('class' =>'links', 'id' => 'navlist'))

And this doesn't print any separator at all. I need the double pipes (||) to appear for my look, but don't know how to do this with 5.x.

Ideas?

Comments

djnz’s picture

Drupal 5 assumes you want your links in an unordered list. If you don't want that, you need to override theme_links by creating a function mytheme_links in template.php in your theme directory (should be sites/all/themes/mytheme/template.php).

The separator is in the line $output .= ' || '; in the code below:

function mytheme_links($links, $attributes = array('class' => 'links')) {
  $output = '';

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

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

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

      // Automatically add a class to each link and also to each LI
      if (isset($link['attributes']) && isset($link['attributes']['class'])) {
        $link['attributes']['class'] .= ' ' . $key;
        $class = $key;
      }
      else {
        $link['attributes']['class'] = $key;
        $class = $key;
      }

      // Add first and last classes to the list of links to help out themers.
      $extra_class = '';
      if ($i == 1) {
        $extra_class .= 'first ';
      } else {
        $output .= '&nbsp;||&nbsp;';
      }
      if ($i == $num_links) {
        $extra_class .= 'last ';
      }
      $output .= '<span class="'. $extra_class . $class .'">';

      // Is the title HTML?
      $html = isset($link['html']) && $link['html'];

      // Initialize fragment and query variables.
      $link['query'] = isset($link['query']) ? $link['query'] : NULL;
      $link['fragment'] = isset($link['fragment']) ? $link['fragment'] : NULL;

      if (isset($link['href'])) {
        $output .= l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment'], FALSE, $html);
      }
      else if ($link['title']) {
        //Some links are actually not links, but we wrap these in <span> for adding title and class attributes
        if (!$html) {
          $link['title'] = check_plain($link['title']);
        }
        $output .= '<span'. drupal_attributes($link['attributes']) .'>'. $link['title'] .'</span>';
      }

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

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

  return $output;
}
smithaa02’s picture

This actually won't work in my case because I have separate instances of my primary menu being displayed but in dramatically different formats.

For anybody that is curious, my final solutions was to code into my template the following:

$firstpass = 1;
foreach ($primary_links as $p) {
if (!$firstpass) {echo " || ";}
$firstpass = 0;
echo "".$p['title']."";
}

darumaki’s picture

Is there any shorter code, seems like a lot just to add a | separator | I only want to add it to the primary links

marknt15’s picture

Hi, I have tried your solution but unfortunately its not working in my zen theme. I mean nothing happened. Is there any other solution? Thanks :)

vjordan’s picture

I have this set up for a site. Instead of having drupal stick in separation characters you do it using the style definitions.

My site uses a theme based on foliage (which in turn was based on bluemarine). I achieve the desired look with the following line in style.css

ul.links li {
  border-left: double #9cf; /*use this for separation line*/
}

It's not exactly the double pipe characters you had previously. But it's a border-style which appears visually similar. You can see the various options you have for styling this "border" at: http://www.w3schools.com/css/tryit.asp?filename=trycss_border-style

You may also have to adjust the border settings for ul.links li.first and ul.links li.last to get this to work properly in your theme.

Gerald Mengisen’s picture

after a little bit of digging, this is my favorite approach. I ended up using

  #primary ul.links li {
  	border-left: thin solid; /*use this for separation line*/
  	padding-right: 0.5em;
  	padding-left: 0.5em;
  }
  
  #primary ul.links li.last {
  	border-right: thin solid; /*use this for separation line*/
  }

for a Zen based theme for displaying it as "| Menu 1 | Menu 2 | Menu 3 |"

marknt15’s picture

Hi gerald, i tried your solution but it did not work in the zen theme. Hhmmm, any ideas? Thanks :)

Gerald Mengisen’s picture

The CSS code above was used in Drupal 5; not sure if this still works in 6. In my case, I created a Zen subtheme and added the CSS code to the zen/[mysubtheme_name]/[mysubtheme_name].css file after where it says
#primary /* Primary links */
{
}

Hope this helps,
Gerald

Littlebob’s picture

Does anyone know of a better way to get a pipe only in a footer menu and not anywhere else?

Thanks.

Gerald Mengisen’s picture

See the comment just above: http://drupal.org/node/113890#comment-913761

and use #footer instead of #primary.

Hope this helps,
Gerald

digidev’s picture

Well, it's not an easy solution and there might be another way around, but this is how I got it working for Drupal6 (menu secondary-links):

function MYTHEME_menu_item_link($link) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }
  
  if ($link['menu_name'] == "secondary-links") {
	$separator = ' &#149;';
  }
  
  return l($link['title'], $link['href'], $link['localized_options']) . $separator;
}


function MYTHEME_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
  $class = ($menu ? 'expanded' : ($has_children ? 'collapsed' : 'leaf'));
  if (!empty($extra_class)) {
    $class .= ' '. $extra_class;
  }
  if ($in_active_trail) {
    $class .= ' active-trail';
  }
  
  if (stripos($extra_class, 'last') !== FALSE && stripos($link, ' &#149;') !== FALSE) {
	$link = str_ireplace(' &#149;', '', $link);
  }
  
  return '<li class="'. $class .'">'. $link . $menu ."</li>\n";
}

I'm quite new to Drupal, so if anyone has improvements to this code, just let me know