views:

929

answers:

4

I recently wrote a theme function to add a class to my primary links that works great. I then wrote some css classes to style these links with custom background images. Worked out great. Now comes the problem, the link text for the primary links still is displayed. Normally this isn't a problem as I would just wrap the in a with a custom "hide" class. For example:

<span class="hide"><a href="#">Link Text</a></span>

So my question is how can I loop through the primary links and wrap the text w/ a <span> like my example? Here's my theme function that I used to add my classes.

function zkc_preprocess_page(&$vars, $hook) {

// Make a shortcut for the primary links variables
  $primary_links = $vars['primary_links'];
// Loop thru the menu, adding a new class for CSS selectors
    $i = 1;

    foreach ($primary_links as $link => $attributes){
        // Append the new class to existing classes for each menu item
        $class = $attributes['attributes']['class'] . " item-$i";
        // Add revised classes back to the primary links temp variable
        $primary_links[$link]['$attributes']['class'] = $class;
        $i++;
        } // end the foreach loop

// reset the variable to contain the new markup
$vars['primary_links'] = $primary_links;

}
+1  A: 

Is jQuery an option?

Try something like this:

$(document).ready(function(){
  $('#primary li a')
  .wrapInner('<span class="hide">' + '</span>');
});

EDIT:

Or if you want to go Drupal, put this guy in your foreach loop:

$link['title'] = '<span class="hide">' . check_plain($link['title']) . '</span>';

Erik Ahlswede
Very interesting! I'll give that a shot right now...
slimcady
The jQuery worked like a charm, thanks! A little dash of css and it works just the way I want. Thanks so much.
slimcady
+1  A: 

If all you want is to hide the link text, why don't you just use something like text-indent: -9999px;?

googletorp
I like this idea too! I'm much better at css than php, so this might work better for me.
slimcady
That's just bad practice..
Kevin
That's just css.
googletorp
+1  A: 

The correct methods for altering the output of the menu links can be done at the theming layer. You were on the right path with the preprocessing hook use, but there is a little more to it.

Refer to this for more information:

http://drupal.org/node/352924#comment-1189890

http://api.drupal.org/api/function/theme_links/6

Kevin
unfortunately i'm using Drupal6 and this guide only applies to 4.7 and 5, kinda old... thanks anyway!
slimcady
Hm, wonder why nothing on Drupal 6 is in there. Check this out http://drupal.org/node/352924#comment-1189890http://api.drupal.org/api/function/theme_links/6
Kevin
A: 

Typo?

$primary_links[$link]['$attributes']['class'] = $class;

Should read;

$primary_links[$link]['attributes']['class'] = $class;

abitofcode