A way to add styling to individual parts of $links

I believe I have figured out a way to customize the individual components of the $links array, by adding css wrappers to them. Using the code from the main Customizing links page in your template.php file, create the links.tpl.php file and put this code into it:

<?php
/**
* to change the delimiter, just modify the $delimiter value
* Add in other fixed links to the link array by adding something like
* $links[] = l('link text', 'link path');
*
*/

$delimiter = "|";
// Display the left cap of the 'button bar'
print ""; // put whatever you want here - using nothing in this case

$link_count = count($links);
$current = 1;

foreach (
$links as $lnk ) {
   
$key = strstr($lnk, 'thumbnail');  // does a search in for thumbnail in the link
   
if ($key !== FALSE) {                 // if it doesn't find the string it will return FALSE, so this checks that its NOT false
     
print "<span class=\"link-thumbnail\">".$lnk."</span>";    // adds a span around link; change the class to whatever you want
   
}   
    else {
    print
"<span class=\"link-$current\">".$lnk."</span>"// for all other links it will create a class=link-2 etc.
   
}
  
// 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 ""; // put whatever you want here
?>

So far this works for me pretty well. You just have to change the search string to whatever you want. In theory (I haven't tried it yet), you can add elseif statements to test for more strings eg. post comment, login, register, etc. and change the class for each of the $links components. I am using span instead of div as I want the links to remain inline. When I used div wrappers, the links appeared on separate lines. I could have corrected it by CSS, but using spans is easier and more correct I think.

There is also another solution in the Drupal forums: http://drupal.org/node/49393#comment-92915. This works very nicely too.

 
 

Drupal is a registered trademark of Dries Buytaert.