As you can see, these menu links have unique icons. Yet another miracle accomplished using Drupal's PHPTemplate. This technique is especially cool because it automatically generates CSS ID's from the menu link's name.
Now create a menu_item.tpl.php file.
Now, we just include some CSS to make it work:
ul.menu {
margin-left:0;
padding:0;
}
ul.menu ul {
margin-left:1.5em;
padding:0;
}
ul.menu li {
list-style-image:none;
list-style-type:none;
margin:0;
padding:0;
}
ul.menu li a{
background-repeat:no-repeat;
padding:0 0 0 1.5em;
background-position:left;
font-size:1.1em;
margin:0;
}
li#my_account a{
background-image:url(../art/menu_links/my_account.png);
}
li#administer a{
background-image:url(../art/menu_links/administration.png);
}
li#my_blog a{
background-image:url(../art/menu_links/my_blog.png);
}
li#create_content a{
background-image:url(../art/menu_links/create_content.png);
}
li#recent_posts a{
background-image:url(../art/menu_links/recent_posts.png);
}
li#log_out a{
background-image:url(../art/menu_links/log_out.png);
}
The icons, and CSS files are included in a downloadable example theme here.
Comments
This is a great tutorial but
This is a great tutorial but could you possibly explain how one would remove the actual text link? So the image would act as the link, hope this makes sense. I've tried to work around the issue but no luck, any advice you could give would be greatly appreciated.
Thanks
Link this to taxonomy, too?
I got this solution working for my main nav. I also wish for my main nav to know which item is "active" based on clicking a node in my secondary nav. That node will be linked by taxonomy to the main nav.
How do I extend this to take what category the node I am viewing is on and tell the main nav which item should be active.
Thanks for any help.
Cheers,
Adam
download not found
Hello,
Would you be able to forward on the attached .zip file. I'd be very grateful. Many thanks in advance.
vilmar@pti.org.br
1-st thanks a lot:) This
1-st thanks a lot:) This solution works fine for Drupal 5.2, but all child menu items have same icon as parent, so i made some changes I'd like 2 share with u;)
menu_item.tpl.php
<?php
$link = menu_item_link($mid);
// replace spaces with "_", and strip HTML
$css_id = str_replace(' ', '_', strip_tags($link));
// render the menu link with unique CSS id.
$output = '
\n
\n";
print $output;
?>
Just what I needed
I knew this was the kind of thing I needed. Thank god I found this site before reinventing the wheel. You're a star.
Not for 5.1?
First, thanks for this cool little tutorial. My main question: Is this not valid for 5.1? I commented out 'function theme_menu_item' in menu.inc, added your 'function phptemplate_menu_item' code to the end of menu.inc and created menu_item.tpl.php. The menu code is being output the same as before - with just an active class for the active link. I'm guessing that either I'm doing something wrong, or something different needs to be done for 5.1.
I have the same problem.
I have the same problem. Nick?
I was confused for a little
I was confused for a little while too.
He meant to say: Add that code in the TEMPLATE.PHP file in your theme- in effect it will override "theme_menu_item()"
Actually solved the localization problem
If I had waited just two moments before pressing submit to my previous post I would have posted the solution right away. Instead of using str_replace(' ', '_', strip_tags($link)); to generate the id I used $mid in the following way:
$css_id = 'menu-item-'.$mid;
It will give you an css id (or class) like id="menu-item-82" for all languages. It's not as nice looking av accessible as the original solution but it seems to be the only way when dealing with multilingual content.
Nice! But how about localization?
This method looks like it's will work just fine for most people. However, if one has a numerous amount of languages this method will cause problems. $css_id = str_replace(' ', '_', strip_tags($link)); will return a separate id for each language. This can come in handy offcourse, but most of the time it wil cause a problem. Is there a better way of doing this for multilingual menus?
The drupal articles you write are great by the way! :)
Very helpful tutorial
I was looking for a way to manipulate menu items based on node types. Not exactly what is described here, but still your tutorial helped me a lot. Looks like it’s worth to visit your page regularly, thanks!
The link to download the file still isn't working
Download not working
Link to downloadable example theme
more than one menu
handling special chars
ID vs CLASS
Actually this happened to me
Problems with Friends Electric
$css_id = str_replace(' ', '_', strip_tags($link));This line causes problems with "Content" menu in the Friends Electric theme, but I can't figure out why...Don’t know… this
I’m looking for a way to
No this method is right on
Any idea how to implement
link to downloadable example theme (still) not working
$(**$&#&! Well, you can
Same thing on book menu ?
I actually don’t think it
I had a look at book.module,
There seems to be a line break missing
$output...be on a new line? As is, it's lost in the comment.remind me not write
link to downloadable example theme not working
Sorry bout that:fixed
Download link not working
I'd like to tinker around with this - although I'm pretty terrible at coding and stuff...(!)
Please, please, please can you make the link active again? or maybe you could email me the file?
Thanks
dj
Ditto
Hi Nick,
Would you be able to forward on the attached .zip file. I'd be very grateful. Many thanks in advance.
ubahnclothing@yahoo.co.uk
Post new comment