Override or add a primary links delimiter in Drupal 5

Last modified: August 18, 2007 - 02:11

Here's how I added the ' | ' character between each link in my primary navigation menu in Drupal 5.

Following the standard template.php override procedure, I copied the theme_links function from theme.inc into my template.php file, and renamed it phptemplate_primarylinks. Then I edited the last few lines of the function from this:

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

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

  return $output;
}

to this:

      $i++;
      $output .= "</li>";
      if ($i <= $num_links) {
        $output .= " | ";
      }
      $output .= " \n";
    }

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

  return $output;
}

Because of the way that the rest of the function works, the last item that is output actually increments the variable 'i' to one more than the value of $num_links, so it's necessary to test for 'less than or equal too'.

The only thing left to do was call the theme function in page.tpl.php like this:

<?php print theme('primarylinks', $primary_links, array('class' => 'links primary-links')) ?>

I didn't try anything other than the pipe character, but I guess you could use anything you want as a delimiter.

Doug Gough

Custom Delimiter for Primary Links - Drupal 5

zorroposada - September 20, 2007 - 08:38

Edit your template.php and add this:

<?php
//custom primary links
function phptemplate_links($links = array(), $attributes = array('class' => 'links'))
{
    return
_phptemplate_callback('primary_links', array('links' => $links, 'attributes' => $attributes));
}
?>

omit the closing php tag.

And then create a file in your theme folder named "primary_links.tpl.php" and paste this code:

<?php

 
/**
* to change the delimiter, just modify the $delimiter value
*
*/

$delimiter = " | ";
// Display the left cap of the 'button bar'
//print "[";
$link_count = count($links);
$current = 1;
foreach (
$links as $lnk ) {
       
// Print the link
    //print_r($lnk); //Array ( [title] => TITLE [href] => # [attributes] => Array ( ) )
   
print l($lnk['title'], $lnk['href'], $lnk['attributes']);
       
// Only print the delimiter if not the last link
   
if ( $current < $link_count ) {
        print
$delimiter;
    }
   
$current++;
}
// Display the right cap of the 'button bar'
//print "]";
?>

This also make a link out of the times-read segment....

Alex Regenbogen - October 25, 2007 - 11:51

I've made a slight modification to the above, because it also makes a link (which of course points nowhere) out of the times-read segment.

<?php
/**
  * to change the delimiter, just modify the $delimiter value
  **/
$delimiter = " , ";
$link_count = count($links);
$current = 1;
foreach (
$links as $lnk ) {
   
// Print the link
    //print_r($lnk); //Array ( [title] => TITLE [href] => # [attributes] => Array ( ) )
   
if (isset($link['href'])) print l($lnk['title'], $lnk['href'], $lnk['attributes']);else print $lnk['title'];
       
// Only print the delimiter if not the last link
   
if ( $current < $link_count ) {
        print
$delimiter;
    }
   
$current++;
}
?>

Just my 2 cents ;)

Adding a delimiter / seperator / divider to primary or secondary

duckxtales - February 29, 2008 - 00:24

Adding a delimiter / seperator / divider to primary or secondary links in page.tpl.php

Here's a much easier way without going into template.php. I used regex and replaced "li- -li" with "li- (your delimiter here) -li". Change the XX to what you want as a divider. Also you will need to edit the style sheet as the li's has a padding on it.

<?php print preg_replace('/li\>(\s\S*?)\<li/', 'li>XX<li', theme('links', $primary_links, array('class' =>'links', 'id' => 'navlist'))) ?>

the above does trick does

duckxtales - February 29, 2008 - 01:01

the above does trick does not work in IE6 and IE7... it causes the delimiter to appear inside the LI... This is becuase the delimiter is appearing between the LI tags.

I had modified it again to have it remove all the LI tags except for the first one and the last one. Which turns the entire thing into just 1 LI line with a bunch of A tags in it.

<?php print preg_replace('/\<\/li\>(\s\S*?)\<li(.*?)\>/', ' XX ', theme('links', $primary_links, array('class' =>'links', 'id' => 'navlist'))) ?>

it will end up looking like this:

<a class="menu-1-1-2" href="#">CU Home</a> XX <a class="menu-1-2-2" href="#">Libraries Home</a>

 
 

Drupal is a registered trademark of Dries Buytaert.